0

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?

5
  • remove ; after BEGIN Commented May 24, 2022 at 15:42
  • Did not help - I removed the BEGIN END; completely because it is no transaction. Commented May 24, 2022 at 15:46
  • Does this answer your question? Postgres syntax error at or near "IF" Commented May 24, 2022 at 16:03
  • 1
    You are trying to run an SQL command named IF, but there is no such SQL command. Maybe you want to use the psql metacommand \if? Commented May 24, 2022 at 17:24
  • @jjanes Thank you, this helped. The documentation of the meta-commands could be better or at least easier to find. Commented May 24, 2022 at 19:13

1 Answer 1

0

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.