I am looking for a solution to execute multiple commands in PSQL.
Lets say I have a file with 2 name for database.
1. slave1
2. slave2
I already have a master template named "master_db
".
If I would do it manually, I will do the following:
CREATE DATABASE slave1 TEMPLATE master_db;
CREATE DATABASE slave2 TEMPLATE master_db;
If there were only 2, it would be very easy but in my case there are more than 30, hence I would like to know if we can create a script. I did try to search but couldn't find a solution.
The databases are in the same cluster, if that matters.
Best Answer
If you are using
psql
9.6+, there is a very convenient command called\gexec
:So, your script could look like:
Note that you can use the newer
psql
version, even if the server version is lower. There might be some rare cases of incompatibility (when using\d*
commands, because some system views may change between server versions), but I haven't yet seen such a case.If you are still stuck on an earlier client version, you can just use the
SELECT
and copy the output manually to a script file.If your database names are in a file, you have multiple options. Either treat that file with an editor (something like
sed -i -e 's/^/CREATE\ DATABASE\ /' -e 's/$/\ TEMPLATE\ master_db'
in any OS wheresed
exists), and then run the resulting script.Alternatively, you can also do something like this:
Important note: only use \gexec when you absolutely trust the source you are working with. The above approach is prone to SQL injection (which, interestingly, not so easy when one of the commands on the same line is
CREATE DATABASE
, but it's always better to be careful than sorry later).