How do I escape single quote in command line query of psql?

本小妞迷上赌 提交于 2019-12-22 05:08:29

问题


I googled a lot but..

How do I escape single quote in command line query of psql ?

psql -t -A -F $'\t' postgresql://zzzz:5432/casedb -U qqqq -c 'select id,ext_ids ->> 'qwe' as qwe from data ORDER BY qwe' > /jdata/qwe.tab

Results in error

ERROR:  column "qwe" does not exist
LINE 1: select id,ext_ids ->> qwe as qwe from data...

回答1:


In Postgres you can use dollar-quoted strings:

select id,ext_ids ->> $$qwe$$ as qwe from data ORDER BY qwe;
-- or
select id,ext_ids ->> $anything$qwe$anything$ as qwe from data ORDER BY qwe;



回答2:


You could just use double quotes (") for the shell quoting and single quotes (') for the SQL quoting:

psql -t -A -F $'\t' postgresql://zzzz:5432/casedb -U qqqq -c "select id,ext_ids ->> 'qwe' as qwe from data ORDER BY qwe" > /jdata/qwe.tab
# Here ------------------------------------------------------^---------------------------------------------------------^


来源:https://stackoverflow.com/questions/38657883/how-do-i-escape-single-quote-in-command-line-query-of-psql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!