0

do you know any way to declare a database name in a variable?

I have tried many methods that I found on the Internet. I am looking for a simple script. I tried to do this in a DO block, but it doesn't work. I just need to declare a variable, set its value, and create a database with a specified name.

Is it even possible to do in PostgreSQL 13?

I will be very grateful for any hint.

MikeRej
  • 1
  • 2
  • 3
    This isn't PostgreSQL syntax, and you can't create a database in a transaction. Something like this could be done in a DO block, but that's a transaction and because of that not possible for CREATE DATABASE – Frank Heikens Mar 08 '23 at 14:01
  • 2
    Furthermore, you can't dynamically/parameterize database objects in SQL. You can write sql into a string variable and then use [`EXECUTE`](https://stackoverflow.com/questions/12780275/dynamic-sql-query-in-postgres) to run it in a procedure. Ultimately though I would question the need for dynamically creating databases, or any database object for that matter, in the first place. – JNevill Mar 08 '23 at 14:50

0 Answers0