I am trying to figure out how to drop multiple databases on PostgreSQL in one sql script. I preferably need it to work both when executed in pgAdmin
console, and when used in psql
command line tool. When I do:
DROP DATABASE db1;
DROP DATABASE db2;
I get following error:
DROP DATABASE cannot be executed from a function or multi-command string
Is there any way around this problem? I.e., is there any way to run each command separately (like when using "GO" statement on MSSQL), or some other sensible way to easily drop multiple databases? I need this functionality for maintenance scripts during development.
Best Answer
When executing multiple commands in a script in pgAdmin they are automatically wrapped into a transaction. You can explicitly
begin
andcommit
transactions, but not with commands that won't run in a transaction context. The manual:Similar in psql when called with
-c command
. The manual:However, when fed to psql via standard input:
So you can use psql with standard input in default autocommit-on mode.
Else, you can only run separate commands.
You could use the shell-command
dropdb
- or write a shell-script with it that drops multiple databases.BTW, the only difference between what you can run from pgAdmin and psql are the meta-commands of psql - which are not SQL. Those are interpreted by psql and not the database engine. To switch between meta-commands and SQL, use the separator meta-command
\\
. There is an example in the manual.