Contents
DROP DATABASE
An SQL command for dropping a database
DROP DATABASE
is a DDL command to drop (permanently delete) a database.
DROP DATABASE
has been present in all PostgreSQL versions.
Change history
- PostgreSQL 13
- can now take the option
FORCE
to drop a database even if other users are connected (commit 1379fd53)
- can now take the option
- PostgreSQL 8.2
IF EXISTS
clause added (commit 5b352d8e)
Examples
Basic execution example for DROP DATABASE
:
postgres=# DROP DATABASE foo; DROP DATABASE
Attempting to drop a database which does not exist:
postgres=# DROP DATABASE bar; ERROR: database "bar" does not exist
Safely attempting to drop a database which might not exist:
postgres=# DROP DATABASE IF EXISTS bar; NOTICE: database "bar" does not exist, skipping
Attempting to drop the current database:
postgres=# DROP DATABASE postgres; ERROR: cannot drop the currently open database
Attempting to drop another database currently in use:
postgres=# DROP DATABASE foo; ERROR: database "foo" is being accessed by other users DETAIL: There are 2 other sessions using the database.
(Note there may be a delay of a few seconds before PostgreSQL determines it cannot drop the database.)
Forcibly dropping another database currently in use (PostgreSQL 13 and later):
postgres=# DROP DATABASE foo WITH (FORCE); DROP DATABASE
References
- PostgreSQL documentation: DROP DATABASE