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

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 ?

An alternative is to use the -c option several times:
psql -c '\x' -c 'SELECT ...'

You can also set the expanded mode from the commandline:
kubectl -n MYNAMESPACEXXX exec -ti MYPGPOD-K8SXXX -- psql -d MY-DB -U postgres --expanded -c 'select * from pg_stat_statements order by max_exec_time desc limit 10;'

Related

KubeCTL with Postgres query doesn't work and throws "LINE 1: select * FROM "employees" where employeeData::text like someSampleHere;"

The field employeeData is of type json.
Consider the query:
select * FROM "employees" where employeeData::text like '%someSampleHere%'
When I'm running the query inside Postgres it works perfect and I get the rows that I'm asking for.
However when I use it with KubeCTL and running this query outside of the PG App
psql -d employess_db -U postgres -c 'select * FROM "employees" where employeeData::text like 'someSampleHere';'
PG throws
ERROR: syntax error at or near "%"
LINE 1: select * FROM "employees" where employeeData::text like %someSampleHere%;
How can we fix it ?
Sounds like a quoting problem to me. You neglected to show your actual kubectl command line in your question, but this works for me without any errors:
kubectl exec postgres-pod -- psql -U postgres -d employees_db \
-c "select * from \"employees\" where employeeData::text like '%someSampleHere%'"

Add `\x on` to PostgresDB query with kubectl exec

I have a PostgresDB query which I want to run directly using kubectl exec:
kubectl exec -it -n <ns> <pod_name> -- psql -U <user> -d <db> -c "select * from <table> where id=1234;"
I want to toggle expanded output (\x on option) only for this command. I do not want to permanently enable this for all queries.
I have tried various things, such as chaining commands together with a semi-colon (i.e. \x on; select * from <table>;). I'm not getting this to work.
How do I get this to work?
There are several ways to do that, but the simplest is to use the -x option of psql.

How to format result when query is executed from bash instead of PSQL shell?

I am familiar with \x auto mode of PSQL and it works great when I make query from inside PSQL shell.
But I'm executing query from bash shell and psql is running inside a docker container.
How can I combine \x auto with SELECT query in such case ?
What I have already tried:
$ docker exec -it my_database psql -U iamuser -c "\x auto; SELECT * FROM mytable;"
Expanded display is used automatically.
\x: extra argument "select" ignored
\x: extra argument "*" ignored
\x: extra argument "from" ignored
\x: extra argument "mytable;" ignored
I also tried doing this, but no query results are not shown.
$ docker exec -it my_database psql -U iamuser -c "\x auto \n SELECT * FROM mytable;"
Expanded display is used automatically.
Is it possible to achieve this ? If yes, how ?

PSQL row limit on export table

I was connecting to another team's Postgres DB and was trying to export data for some debugging purposes.
I noticed when I ran the below command, I got 50K rows
psql -t "sslmode=require host=MY_HOST port=MY_PORT dbname=MY_DB user=MY_USER password=MY_PASSWORD" -c "SELECT * FROM MYTABLE;" -o EXPORT.txt
$ cat EXPORT.txt | wc -l
50000
But when I log in and check the row count, it was 94K
psql -t "sslmode=require host=MY_HOST port=MY_PORT dbname=MY_DB user=MY_USER password=MY_PASSWORD"
MY_DB=> SELECT count(*) FROM MYTABLE;
94508
What is the row count for exporting the table using psql shell?
Is this a hard limit?
I wrote a bash script to loop using limit and offset as a work around.

How to return a value from psql to bash and use it?

Suppose I created a sequence in postgresql:
CREATE SEQUENCE my_seq;
I store the below line in an sql file get_seq.sql
SELECT last_value FROM my_seq;
$SUDO psql -q -d database_bame -f get_seq.sql
How do I get the int number returned by SELECT into bash and use it?
You can capture the result of a command using the VAR=$(command) syntax:
VALUE=$(psql -qtAX -d database_name -f get_seq.sql)
echo $VALUE
The required psql options mean:
-t only tuple
-A output not unaligned
-q quiet
-X Don't run .psqlrc file
Try:
LAST_VALUE=`echo "SELECT last_value FROM my_seq;" | psql -qAt -d database_name`