When you no longer need a database, drop it: PostgreSQL deletes the database and everything in it. Here’s how to drop a database in PostgreSQL, what IF EXISTS and WITH (FORCE) add, and the errors you can meet.
Every command and output on this page is real, from PostgreSQL 19 on macOS.
Warning: DROP DATABASE can’t be undone. Check the name twice before you run it.
Syntax
DROP DATABASE [ IF EXISTS ] name [ WITH ( FORCE ) ];
- Parts in square brackets are optional.
name: the database to drop.IF EXISTS: don’t fail when there’s no database by that name; PostgreSQL prints a notice instead.WITH (FORCE): first end every other connection to the database, then drop it.- You must own the database or be a superuser, such as
postgres, and you can’t drop the database you’re connected to.
These come from DROP DATABASE (opens in a new tab) in the PostgreSQL documentation.
Step 1: Connect to Another Database
Connect to your server with psql as the postgres user, as Part 2 of the PostgreSQL Tutorial shows. psql connects you to the postgres database, and its prompt is postgres=#.
This guide drops a database called scratch. To follow along, create it first:
CREATE DATABASE scratch;
CREATE DATABASE
Step 2: Drop the Database
DROP DATABASE scratch;
DROP DATABASE
The database and everything in it are gone.
Step 3: Use IF EXISTS in Scripts
Dropping a database that doesn’t exist is an error:
DROP DATABASE scratch;
ERROR: database "scratch" does not exist
IF EXISTS turns the error into a notice, which is handy in a script that may run more than once:
DROP DATABASE IF EXISTS scratch;
NOTICE: database "scratch" does not exist, skipping
DROP DATABASE
Step 4: Drop a Database Others Are Still Using
DROP DATABASE refuses while anyone else is connected to the database, such as an app, pgAdmin or another psql window. To see it, create scratch again:
CREATE DATABASE scratch;
CREATE DATABASE
Then open a second Terminal window and connect to scratch there:
psql -U postgres -d scratch
Back in the first window, try to drop it:
DROP DATABASE scratch;
ERROR: database "scratch" is being accessed by other users
DETAIL: There is 1 other session using the database.
Close the other connections, or add WITH (FORCE), which ends them first:
DROP DATABASE scratch WITH (FORCE);
DROP DATABASE
The second window has lost its connection, and psql says so the next time you run anything there:
SELECT 1;
FATAL: terminating connection due to administrator command
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.
The connection to the server was lost. Attempting reset: Failed.
!?>
The prompt changes to !?>: the ! means psql is no longer connected to a database, as the psql documentation (opens in a new tab) explains. Type \q to quit. Because FORCE cuts other people off like this, use it only when you’re sure nobody needs the database.
Note: FORCE can’t end every connection. It fails when the database has prepared transactions, active logical replication slots or subscriptions, as the documentation (opens in a new tab) explains.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Dropping the database you’re connected to | ERROR: cannot drop the currently open database | Connect to another database first: \c postgres. |
| Dropping a database that doesn’t exist | ERROR: database "scratch" does not exist | Check the name, for example with \l as How to List Databases in PostgreSQL shows, or add IF EXISTS. |
FAQ
Can I get a dropped database back?
No. DROP DATABASE deletes the directory that holds the database’s data, as the documentation (opens in a new tab) says. The only way back is a backup made before the drop.
Is DROP the same as DELETE?
No. DELETE removes rows from a table and keeps the table. DROP DATABASE removes the whole database.
Conclusion
Connect to another database, check the name, and drop the database with DROP DATABASE. Add IF EXISTS in scripts, and WITH (FORCE) only when other connections must be ended first.
Additional Resources
- PostgreSQL 19 documentation: DROP DATABASE (opens in a new tab)
- PostgreSQL 19 documentation: Destroying a Database (opens in a new tab)
Related
- PostgreSQL Tutorial — Part 4: Create a Database
- How to Create a Database in pgAdmin 4
- How to Rename a Database in PostgreSQL
Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.
0 comments