Search code examples
postgresqlpsqlkubectl

Run multiple commands in one line fails when using "\x" with PSQL (POSTGRES)


I'm trying to run the following queries in one line:

\x
select * from pg_stat_statements order by max_exec_time desc limit 10;

As follows:

kubectl -n MYNAMESPACEXXX exec -ti MYPGPOD-K8SXXX -- psql -d MY-DB -U postgres -c '\x select * from pg_stat_statements order by max_exec_time desc limit 10;'

But I get

unrecognized value "select" for "expanded"
Available values are: on, off, auto.
command terminated with exit code 1

How can we combine both \x and the SQL query ?


Solution

  • An alternative is to use the -c option several times:

    psql -c '\x' -c 'SELECT ...'