1

I am using psql command to call a file bypassing argument as -v argument=value. In the SQL file, the argument needs to be used as part of a string. For example prefix_:argument_suffix. This is not working. If I use it as prefix_:argument it works, but when it comes to the suffix it doesn't.

This is for building a database name in the create database statement.

Can somebody help with this?

Laurenz Albe
256k21 gold badges307 silver badges382 bronze badges
asked Oct 19, 2020 at 17:40

2 Answers 2

1

Use another variable like:

nd@postgres=# \set argument 'aaa'
nd@postgres=# \set dbname 'foo_':argument'_bar'
nd@postgres=# select :'dbname';
┌─────────────┐
│ ?column? │
├─────────────┤
│ foo_aaa_bar │
└─────────────┘
nd@postgres=# create database :dbname;
CREATE DATABASE
nd@postgres=# \l
 List of databases
┌─────────────┬──────────┬──────────┬─────────────┬─────────────┬───────────────────────┐
│ Name │ Owner │ Encoding │ Collate │ Ctype │ Access privileges │
├─────────────┼──────────┼──────────┼─────────────┼─────────────┼───────────────────────┤
│ foo_aaa_bar │ nd │ UTF8 │ en_US.UTF-8 │ en_US.UTF-8 │ │
│ postgres │ postgres │ UTF8 │ en_US.UTF-8 │ en_US.UTF-8 │ │
│ template0 │ postgres │ UTF8 │ en_US.UTF-8 │ en_US.UTF-8 │ =c/postgres ↵│
│ │ │ │ │ │ postgres=CTc/postgres │
│ template1 │ postgres │ UTF8 │ en_US.UTF-8 │ en_US.UTF-8 │ =c/postgres ↵│
│ │ │ │ │ │ postgres=CTc/postgres │
└─────────────┴──────────┴──────────┴─────────────┴─────────────┴───────────────────────┘
answered Oct 20, 2020 at 6:10
Sign up to request clarification or add additional context in comments.

5 Comments

In Oracle we have prefix_&&argument._suffix, anything similar in Postgres? I need to handle it in the statement itself.
@JeZ As I know, no. postgresql.org/docs/current/…
In that case, let me set the parameters as you have shown. Thank you
@JeZ Yet another way is to set prefix/suffix variables and then use it like create database :prefix:argument:suffix. Choose the case most suitable for you. Good luck!
In my case, there are multiple prefixes and suffixes which are constants. So I am going with your first answer. That would suffice. Thank you
0

The string could be built as

'prefix_' || :'variable' || '_suffix'

To use it in a CREATE DATABASE statement, you will have to use dynamic SQL in a function:

CREATE FUNCTION pg_temp.mkdb(var text) RETURNS void
 LANGUAGE plpgsql STRICT AS
$$BEGIN
 EXECUTE format('CREATE DATABASE %I;', 'prefix_' || var || '_suffix');
END;$$;
SELECT pg_temp.mkdb(:'variable');

Since the function is created in the temporary schema, it will exist only in this database session.

answered Oct 19, 2020 at 19:06

3 Comments

This is for building a database name in the create database statement. This won't work in that case. Could you please help with that case
You should have said that in the question. See my extended answer.
Is there any possibility to do it without a function? In Oracle we have prefix_&&argument._suffix, anything similar in Postgres?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.