Skip to main content
0 reactions

How to Drop a Database in PostgreSQL

Card for How to Drop a Database in PostgreSQL, with a warning sign and two facts: DROP DATABASE scratch; and WITH (FORCE) ends sessions.
How to Drop a Database in PostgreSQL.

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

SyntaxSQL
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:

postgres=#SQL
CREATE DATABASE scratch;
OutputPlain text
CREATE DATABASE

Step 2: Drop the Database

postgres=#SQL
DROP DATABASE scratch;
OutputPlain text
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:

postgres=#SQL
DROP DATABASE scratch;
OutputPlain text
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:

postgres=#SQL
DROP DATABASE IF EXISTS scratch;
OutputPlain text
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:

postgres=#SQL
CREATE DATABASE scratch;
OutputPlain text
CREATE DATABASE

Then open a second Terminal window and connect to scratch there:

Terminal 2Bash
psql -U postgres -d scratch

Back in the first window, try to drop it:

postgres=#SQL
DROP DATABASE scratch;
OutputPlain text
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:

postgres=#SQL
DROP DATABASE scratch WITH (FORCE);
OutputPlain text
DROP DATABASE

The second window has lost its connection, and psql says so the next time you run anything there:

scratch=# (Terminal 2)SQL
SELECT 1;
OutputPlain text
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

MistakeWhat happensDo this instead
Dropping the database you’re connected toERROR: cannot drop the currently open databaseConnect to another database first: \c postgres.
Dropping a database that doesn’t existERROR: database "scratch" does not existCheck 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

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