Removes a database, including its catalog entries and data directory.
Synopsis
DROP DATABASE [ IF EXISTS ] name [ [ WITH ] ( option [, ...] ) ]
where option can be:
FORCERequired privileges
Only the database owner can run DROP DATABASE.
To use the FORCE option, you must have the same permissions required by pg_terminate_backend.
Parameters
| Parameter | Description |
|---|---|
IF EXISTS | Do not throw an error if the database does not exist. A notice is issued instead. |
name | The name of the database to remove. |
FORCE | Attempts to terminate all existing connections to the target database before dropping it. Fails if the current user lacks permission to terminate other connections, or if the database has prepared transactions, active logical replication slots, or subscriptions. |
Usage notes
DROP DATABASEcannot be undone. Back up any data you need before running this command.Run this command while connected to a different database (such as
postgres). It cannot be run while connected to the target database.If other clients are connected to the target database, the command fails unless you use the
FORCEoption.DROP DATABASEcannot run inside a transaction block.As an alternative, use the
dropdbcommand-line program, which wraps this command and is useful when you are already connected to the target database.
Examples
Drop a database
DROP DATABASE mydb;Drop a database only if it exists
Without IF EXISTS, dropping a non-existent database returns an error:
DROP DATABASE non_existing_db;ERROR: database "non_existing_db" does not existWith IF EXISTS, a notice is issued and the command succeeds:
DROP DATABASE IF EXISTS non_existing_db;NOTICE: database "non_existing_db" does not exist, skipping
DROP DATABASEDrop a database with active connections
If other sessions are connected to the target database, DROP DATABASE fails. Use FORCE to terminate those connections first:
DROP DATABASE mydb WITH (FORCE);FORCE does not terminate connections blocked by prepared transactions, active logical replication slots, or subscriptions. Resolve those before retrying.