Question

UPDATE: This specific problem is not related to psql included scripts. Variables in the included scripts ARE interpolated as expected. Actual problem results from the use of psql variables inside $$ quotes in SQL function definitions and is described in this question.

I am using psql variables to set some parameters (table names, values) from the command line in a rather long SQL script. The script has become too long and I splitted it into several parts and included them into the main script using \ir subscript.sql syntax. However, variables from the main script are not passed to the subscripts. This is strange because documentation says that \i file is an equivalent of typing file on the keyboard, i.e., the script environment should be preserved.

Is there a simple way to pass variables to the included scripts in psql?

I am using psql version 9.3.1.

Was it helpful?

Solution

Have a look at --variable= at http://www.postgresql.org/docs/current/static/app-psql.html

Eg.

psql -d somedb -U someuser -f yourscript.sql --variable=Foo=foobar

In your script you could have:

UPDATE sometable SET somecol = :Foo;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top