Charles Duffy
Charles Duffy

Reputation: 295443

psql command-line variable interpolation leads to syntax error

From the psql command documentation, the --variable command described as making variables available for substitution with :name syntax. However,

psql --variable=var="'hello'" -c 'select :var'

...results in a syntax error:

ERROR:  syntax error at or near ":"
LINE 1: select :var

Upvotes: 0

Views: 274

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295443

This works correctly if the query text is fed in on stdin in bash:

psql --variable=var="'hello'" <<<"select :var"

Or by using POSIX sh:

psql --variable=var="'hello'" <<<'EOF'
select :var
EOF

Upvotes: 1

Related Questions