How to escape quotes in docker exec bash command - postgresql

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"

Related

Issue running pg_dump from within package.json with specified tables possibly due to escaped quotes - on Windows

I'm experiencing an issue running a pg_dump command from within a package.json script
My hypothesis is that it has something to do with escaping quotes.
Environment = Windows
Here is the setup to reproduce.
Try running this command from a Bash terminal on Windows. Replace your_database, your_schema and YourTable with your own settings relavant to your own environment..
pg_dump -U postgres -d your_database -n your_schema -a -F p -f ./your_file.sql -t '"YourTable"'
For me, this works fine. ✅
Try using the same command from within a package.json file (note the need to escape the double quotes surrounding the name of YourTable to conform to proper json string)...
{
"name": "test_pg_dump_script",
"version": "0.0.1",
"scripts": {
"db-dump-test": "pg_dump -U postgres -d your_database -n your_schema -a -F p -f ./your_file.sql -t '\"YourTable\"'"
}
}
Then try run this script using the command from your terminal npm run db-dump-test
For me, this does not work. ❌ It results in the following
> test_pg_dump_script#0.0.1 db-backup-test-data
> pg_dump -U postgres -d your_database -n your_schema -a -F p -f ./your_file.sql -t '"YourTable"'
pg_dump: error: no matching tables were found
To try and solve this, I have attempted various combinations of quotes around the YourTable section in the package.json script, alas to no avail. e.g. I've tried removing this single quotes, removing the double quotes, having no quotes at all, etc..
I'm wondering if anyone else might have an insight as to what is going on here?

K8S How to add quotes when on parameters provided for `kubectl exec -c`

I am using K8S
I want to calculate a string that that is a result of kubectl exec -it ... -c
after the -c option there is a string.
How can I pass a string with double quotes inside.
The following example doesn't work properly.
x="$(kubectl exec -it mysql-pod -- /bin/sh -c \"mysql -uroot -p12345
-e 'show databases'\" 2>/dev/null)"
echo $x
Thanks.
when only a command needs to be executed on a pod , -it option is not required as it stands for attaching an interactive teminal
when mysql is itself an executable command , no need to use /bin/sh -c
no need to encapsulate whole command in " "
So try following
x=$(kubectl exec mysql-pod -- mysql -uroot -p12345 -e 'show databases ;' 2>/dev/null)
echo $x

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 ?

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