3

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...
Nolan
  • 748
  • 7
  • 11
  • 1
    I would switch to using double quotes in the outer layer... much easier to get the shell to interpret it how you want. Try this `docker container exec 1b8 bash -c "psql -c \"SELECT * FROM information_schema.tables where table_name='user_mappings'\" > /tmp/output" ` If you really want to use single quotes... check [this question](https://stackoverflow.com/questions/1250079/how-to-escape-single-quotes-within-single-quoted-strings). – kthompso Apr 16 '21 at 18:17
  • Could you perform this SQL query directly in your application code, instead of via a debug shell? Or, can you run `psql` directly on your host, without `docker exec` or the `bash -c` wrapper? – David Maze Apr 16 '21 at 20:44

1 Answers1

4

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"
Ashok
  • 3,190
  • 15
  • 31