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

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 ?

Related

How to escape quotes in docker exec bash command

I am trying to execute a bash command in docker but I'm having trouble figuring out how to escape the single quotes around the table name user_mappings in my command. I've tried backslashes, double single quotes, and double quoted single quotes.
I know I could get around it if I didn't wrap the command in bash -c, but I need that so I can redirect the output to a file in the container.
# docker container exec 1b8 bash -c 'psql -c "SELECT * FROM information_schema.tables where table_name='user_mappings'" > /tmp/output'
ERROR: column "user_mappings" does not exist
LINE 1: ...* FROM information_schema.tables where table_name=user_mappi...
run it from inside the container by
docker exec -it postgres_container_name "psql your_connection_string"
In your case
docker container exec 1b8 bash -c "psql -c \"SELECT * FROM information_schema.tables where table_name='user_mappings'\" > /tmp/output"

postgres script silently pass without any result

i am trying to execute psql queries from the bash command line passing password in following format
set PGPASSWORD=rtttttul psql -U ostgres -h localhost -d postgres -c "select * from logs" -o output.txt
Somehow my queries are not giving any results.i have tried to pass different queries or incorrect credentials but still script execute without any error.
If i don't pass password and try logging in to command prompt,everything works fine.
i want to check what basic thing i am missing above
Below command worked
PGPASSWORD=rtttttul psql -U ostgres -h localhost -d postgres -c "select * from logs" -o output.txt
remove set at start of command fixed it

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

How to escape quotes in a docker postgres command

I have the following command that I'm running (renamed some variables):
docker exec docker_name sh -c 'psql dbname -U joeadmin -c "update table set field='really_longstringwithabunf3493829#########=';"'
When I run this, it will throw the following error:
ERROR: syntax error at or near ";"
LINE 1: ...longstringwithabunf3493829#########=;
^
How can I escape or retain the single quotes so this will work. Also, if this is just a horrible way of approaching this, I'm open to other suggestions. The use case is making changes to a db on a docker container before exporting data out of it.
Working with strings can be a bit ugly at times with Docker. In your case, I would suggest using double-quotes first, then escape inner double-quotes with \:
docker exec docker_name sh -c "psql dbname -U joeadmin -c \"update table set field='really_longstringwithabunf3493829#########=';\""
A cleaner option would be to create an environment variable:
STR="really_longstringwithabunf3493829#########="
docker exec docker_name sh -c "psql dbname -U joeadmin -c \"update table set field='${STR}';\""
Bear in mind that string interpolation happens in the host OS, not in the container (unless you escape the dollar-sign with \${STR}
Still cleaner would just be to create a file and then copy it into the container:
echo "update table set field='really_longstringwithabunf3493829#########=';" > ~/myfile.txt
docker cp ~/myfile.txt docker_name:/tmp
docker exec docker_name sh -c "psql dbname -U joeadmin -f /tmp/myfile.txt"

Pass command line args to sql (Postgres)

How can I pass command line args to sql files ran with psql (Postgres)?
i.e.
psql mydatabase < mysqlfile.sql arg1 arg2 arg3...
Is this possible?
Use variable interpolation feature in psql.
If you specify -v variable1=value1 or --set variable1=value1 parameter on command line, then :variable1 in the sql file will be replaced with corresponding text value.
Note: use standard-SQL quoted strings if you need quotes, spaces and so on.
Example:
echo "SELECT :arg1 FROM :arg2 LIMIT 10;" > script.sql
psql mydatabase -v arg1=relname -v arg2=pg_class < script.sql
psql mydatabase -v arg1="'some string' as label" -v arg2=pg_namespace < script.sql