Search code examples
stringpostgresqlconcatenationpsqlprompt

Postgres: using concatenation in the text displaed by \prompt


In psql, is there any way to have the \prompt text be a concatenation of strings and variables? For example, suppose that in a file test.sql we have:

\set name table_customers
\echo :name 
\prompt CONCAT('The table ', table_customers, ' was created') answer
\echo :answer

The above does not work:

myuser=# \i /home/myuser/test.sql
table_customers
CONCAT(The table ,
psql:/home/myuser/test.sql:3: invalid variable name: "table_customers,"
:answer

I also tried:

\set name table_customers
\echo :name 
\prompt 'The table ' || table_customers || ' was created' answer
\echo :answer

But it also does not work:

myuser=# \i /home/myuser/test.sql
table_customers
The table
psql:/home/myuser/test.sql:3: invalid variable name: "||"
:answer

Substituting "||" with "+" gives the exact same.

What would be the correct way to do that?


Solution

  • I actually just found a working solution: just concatenate into another variable first (using simple spaces) and then pass that new variable as the \prompt text:

    \set name table_customers
    \echo :name
    \set input 'The table ' :name ' was created.'
    \prompt :input answer
    \echo :answer