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

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%'"

Related

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 ?
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;'

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 ?

Set array using psql interface

I have a function that adds users to the database from an array, e.g.
psql -c "SELECT addUsersFromList(array['userA','userB','userC'])"
This works fine, however I would prefer to be able to run this as a script, like so:
psql -f add_users.sql -v userlist=array['userA','userB','userC'])
And add_user.sql:
SELECT addUsersFromList(:userlist);
When I execute the above psql command, I get an error:
psql:Scripts/add_users.sql:39: ERROR: column "userA" does not exist
This seems to be an issue with how I use the -v flag. I had a look at the postgres documentaion on -v, \set and the Variables section on that same page, but could not find a way to assign an array.
Try to write the array as a string literal:
psql -f add_users.sql -v "userlist='{userA,userB,userC}'"

PostgreSQL: ERROR: syntax error at or near "clear"

I use psql to connect to the PostgreSQL database on terminal. If I run a simple select query,
select count(*) from my_schema.my_table;
I get the error:
ERROR: syntax error at or near "clear"
LINE 1: clear
UPDATE:
I cannot even execute a simple select query like select * from my_schema.my_table; or select * from my_table;. It gives me the error:
ERROR: relation "my_table" does not exist
LINE 1: select * from my_table;
Did you login into right database.
The error 'relation "my_table" does not exist' seems cause by wrong database selection.
For example:
psql -h localhost -p 5435 -U myuser -W <>

Store PostgreSQL query result to Shell or PostgreSQL variable

For instance, I have a table stores value:
select * from myvalue;
val
-------
12345
(1 row)
How can I save this 12345 into a variable in postgresql or shell script?
Here's what I tried in my shell script:
var=$(psql -h host -U user -d db <<SQLSTMT
SELECT * FROM myvalue;
SQLSTMT)
but echo $var gives me:
val ------- 12345 (1 row)
I've also tried
\set var (select * from myvalue)
in psql and when I type \set it lists:
var = '(select*frommyvalue)'
No, no, no! Use "raw data" switch from psql, like "-t" or "\t" and pipe the query to psql instead of parsing ascii-table, come on :-)
echo 'select * from myvalue;' | psql -t -h host -U user -d db
If you really need parse psql output, you could also use -H switch ( turns on HTML output ), and parse it with some perl module for parsing html tables, I used that once or twice.. Also, you may want to use a pgpass file and ~/.psqlrc for some defaults, like default DB to connect, when not specified.
psql has a -c/--command= option to accept SQL from the command line, and -t/--tuples-only option to control output formatting.
$ psql -c 'select 1+1'
?column?
----------
2
(1 row)
$ psql -t -c 'select 1+1'
2
$ VALUE=`psql -t -c 'select 1+1'`
$ echo $VALUE
2
var=`psql -Atc "select 1;"`
echo $var
1
In this answer I explain one way to do it, using a co-process to communicate back-and-forth with psql. That's overkill if all you need is to run a query and get a single result, but might be good to know if you're shell scripting with psql.
You can filter the result you get with your psql command:
var=$(psql -h host -U user -d db <<SQLSTMT
SELECT * FROM myvalue;
SQLSTMT)
var=$(cut -d' ' -f3 <<<$var)
None of these worked for me, but this did:
median_avm=psql "host=${dps1000} port=#### dbname=### user=${reduser} password=${redpass}" -c "SELECT AVG(column) FROM db.table;" -t
using a source file with ${dps1000}, ${reduser}, ${redpass} defined and manually entering port and dbname