DROP DATABASE
is a very special command that cannot be undone. To my knowledge there is no way to drop a database inside a transaction. I quote the manual:
DROP DATABASE cannot be executed inside a transaction block.
Whenever you run two When executing multiple commands in a script in pgAdmin, they are automatically wrapped into a transaction. You can explicitly beginbegin
and commitcommit
transactions, but you cannot send multiplenot with commands at oncethat won't run in one session and "outside" of a transaction context. The manual:
DROP DATABASE
cannot be executed inside a transaction block.
Similar in psql when called with -c command
,. per documentation:The manual:
If the command string contains multiple SQL commands, they are processed in a single transaction, unless there are explicit
BEGIN
/COMMIT
commands included in the string to divide it into multiple transactions.
However, when fed to psql via standard input:
This is different from the behavior when the same same string is read from a file or fed to psql's standard input [...]
, because then psql sends each [.SQL command separately.Because of this behavior, putting more than one SQL command in a single
-c
string often has unexpected results.] either It's better to use repeated-c
commands or feed multiple commands to psql's standard input, either using echo as illustrated above, or via a shell here here-document, for example:psql <<EOF \x SELECT * FROM foo; EOF
So you can use psql with standard input in default autocommit-on modeautocommit-on mode.
Else, you can only run this as a separate commandcommands or wrapped into a BEGIN
/COMMIT
block separately.
You could use the program dropdb
from the shell-command dropdb
- or write a shell-script with it that drops multiple database in successiondatabases.
BTW, the only difference between what you can run from pgAdmin and from psql
psql are the meta-comnmands of psqlmeta-commands of psql - which are not SQL. Those are interpreted by psql and not sent to the database engine. You cannot mixTo switch between meta-commands and SQL. (Well, there are tricks with theuse the separator meta-command \\
, there. There is an example in the manual ..example in the manual.)