Escaping single quotes in shell for postgresql Escaping single quotes in shell for postgresql shell shell

Escaping single quotes in shell for postgresql


What I usually do is use double quotes (") for postgres -c's argument and escaped double quotes (\") for psql -c's argument. That way, I can use single quotes (') inside the SQL string with no problem:

[root@mycomputer ~]# su postgres -c "psql -c \"SELECT 'hi'  \" " ?column? ---------- hi(1 row)


Simplest way is to use a 'here document' , which ignores all quoting:

#!/bin/shDB_NAME=my_data_basepsql -U postgres postgres <<STOP_ITcreate database $DB_NAME template=template0  encoding='utf8'  owner=aaa  lc_collate='cs_CZ.utf8'  ;STOP_IT