DROP DATABASE

A DDL command to drop 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

Examples

Basic DROP DATABASE execution:

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

Categories

DDL

See also

dropdb, CREATE DATABASE, ALTER DATABASE