如何在psql的命令行查询中转义单引号?

时间:2016-07-29 11:38:32

标签: postgresql psql quoting

我google了很多但是..

如何在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

导致错误

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

2 个答案:

答案 0 :(得分:5)

在Postgres中,您可以使用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;

答案 1 :(得分:3)

您可以使用双引号(")进行shell引用,使用单引号(')进行SQL引用:

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