Skip to main content
2 of 4
clarify; deleted 3 characters in body; added 7 characters in body
Erwin Brandstetter
  • 182.2k
  • 28
  • 457
  • 620

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 commands in a script, they are automatically wrapped into a transaction. You can explicitly begin and commit transactions, but you cannot run multiple commands in one session "outside" of a transaction.

Therefore, you can only run this as a separate command. You could use the program dropdb from the shell - or write a shell-script with it that drops multiple database in succession.

BTW, the only difference between what you can run from pgAdmin and from psql are the meta-comnmands of psql - which are not SQL. Those are interpreted by psql and not sent to the database engine. You cannot mix meta-commands and SQL.

Erwin Brandstetter
  • 182.2k
  • 28
  • 457
  • 620