Home > Software design >  How to use a argument from bash call in postgresql script?
How to use a argument from bash call in postgresql script?

Time:05-27

I have the following bash script call.

psql -U postgres postgres < ../dump_script.sql -v test=1 >/dev/null

In the dump_script.sql-script I have the following code:

\set dbName 'db_dev'

IF :test = 1 THEN
    dbName := 'db_test'
END IF;

If I don't use the test parameter everything works as expected, the regular dev database is build - if I feed test to the script the following error is thrown.

ERROR:  syntax error at or near "IF"
LINE 1: IF 1 = 1 THEN

What am I doing wrong here?

CodePudding user response:

This answer is possible because of the hint from @jjanes

\set dbName 'dbDev'

SELECT :test = 1 AS is_test_run \gset

\if :is_test_run
    \set dbName 'dbTest'
\endif

This works as intended!

  • Related