What it means
Every PostgreSQL session is connected to exactly one database and can’t switch to another. A database can only be dropped when no session is using it, and that includes yours, so PostgreSQL refuses straight away:
ERROR: cannot drop the currently open database
Nothing was changed. The fix is to run the same command from a session connected to some other database on the same server.
Common causes
- Dropping from inside it: you connected to
shopto look around, then ranDROP DATABASE shopin the same window. - A client or script whose connection names the database it’s dropping, such as a reset script
using the app’s own
DATABASE_URL. - A test or setup tool configured with the test database as its maintenance database.
How to fix it
Connect to another database, then drop
Every server has a database called postgres, made for this kind of maintenance:
psql -h <host> -U <user> -d postgres -c 'DROP DATABASE shop'
In psql you can switch first with \c postgres (which opens a new session in postgres), then run
DROP DATABASE shop;. Any other database you can connect to works as well.
Or use dropdb
dropdb connects to a maintenance database for you (postgres, or template1 if that fails):
dropdb -h <host> -U <user> shop
dropdb -h <host> -U <user> --force shop # PostgreSQL 13+: also ends other sessions
If it then says other users are connected
Once you’re not the one using it, other sessions may still be: see database is being accessed by other users.
Not inside a transaction
DROP DATABASE can’t run inside BEGIN … COMMIT either; some tools wrap everything in a
transaction, and then the error is DROP DATABASE cannot run inside a transaction block.
Reproduce it
In a throwaway PostgreSQL 18.6 container, connected to a database named shop:
DROP DATABASE shop;
SELECT current_database();
ERROR: cannot drop the currently open database
current_database
------------------
shop
(1 row)
From a session connected to postgres, DROP DATABASE IF EXISTS shop then worked. With
\set VERBOSITY verbose, psql shows the code:
ERROR: 55006: cannot drop the currently open database.
In Inlet
On connections Inlet protects, a DROP in the query editor asks before it runs and says why. Run
the DROP DATABASE from a connection whose database is a different one, such as postgres; when a
statement fails, Inlet shows the server’s error, and What this error means opens this page.