Referring to session variables (\\set var='value') from PL/PGSQL
I can pass variables into PostgreSQL using psql --variable="var='value'" <<<'SELECT :var' ...and refer to them as, in this case, :var in SQL queries passed to psql on stdin. However, this doesn't work from code using PL/PGSQL: psql --variable=var="'value'" <<'EOF' DO $$ BEGIN SELECT :var; END; $$ EOF ...yielding the error: ERROR: syntax error at or near ":" How can this be resolved? You cannot to use a psql variables inside plpgsql code directly. The symbol substitution is blocked inside strings: postgres=> select :'xx'; ?column? ---------- AHOJ (1 row) postgres=> select ' :xx '; ?column? ----