expert
expert

Reputation: 30145

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

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...

Upvotes: 11

Views: 10795

Answers (2)

klin
klin

Reputation: 121854

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;

Upvotes: 12

Mureinik
Mureinik

Reputation: 312136

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 ------------------------------------------------------^---------------------------------------------------------^

Upvotes: 4

Related Questions