Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17
SQLSTATE 55006 (object_in_use) on DROP DATABASE means at least one other session is connected to the database you want to drop. The DETAIL line tells you how many: There is 1 other session using the database.
It does not matter what that session is doing. In our tests one session was just idle and the other was inside an open transaction, and both blocked the drop right away (no waiting). Below are the two fixes we ran on PostgreSQL 17: terminate the sessions first, or drop with WITH (FORCE).
01Another user is still connected to the database (even an idle one)
Session A (lab_a) connected to shop with \c shop and ran one SELECT. Checking pg_stat_activity WHERE datname = 'shop' from another session showed A with state idle. When postgres then ran DROP DATABASE shop;, it failed immediately with 55006 and DETAIL: There is 1 other session using the database.
The fix was to end those sessions first with SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'shop';. It returned t, and the following DROP DATABASE shop; succeeded.
Setup
DROP DATABASE IF EXISTS shop WITH (FORCE);
CREATE DATABASE shop;NOTICE: 00000: database "shop" does not exist, skipping
DROP DATABASE
CREATE DATABASE\c shop
SELECT current_database();You are now connected to database "shop" as user "lab_a".
current_database
------------------
shop
(1 row)SELECT pid, datname, usename, state FROM pg_stat_activity WHERE datname = 'shop'; pid | datname | usename | state
------+---------+---------+-------
4325 | shop | lab_a | idle
(1 row)DROP DATABASE shop;ERROR: 55006: database "shop" is being accessed by other users
DETAIL: There is 1 other session using the database.Check who is connected before you terminate: pg_terminate_backend ends the session and rolls back any work it has in progress.
\c shop
SELECT current_database();You are now connected to database "shop" as user "lab_a".
current_database
------------------
shop
(1 row)SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'shop';
DROP DATABASE shop; pg_terminate_backend
----------------------
t
(1 row)
DROP DATABASE02A session with an open transaction is connected; drop in one step with WITH (FORCE)
This time session A connected to shop, ran BEGIN and a SELECT, and left the transaction open. A plain DROP DATABASE shop; failed with the same 55006 and There is 1 other session using the database.
DROP DATABASE shop WITH (FORCE); finished with DROP DATABASE while A was still connected: it terminates the connected sessions and then drops the database in one statement.
Setup
DROP DATABASE IF EXISTS shop WITH (FORCE);
CREATE DATABASE shop;NOTICE: 00000: database "shop" does not exist, skipping
DROP DATABASE
CREATE DATABASE\c shop
BEGIN;
SELECT now() IS NOT NULL AS in_tx;You are now connected to database "shop" as user "lab_a".
BEGIN
in_tx
-------
t
(1 row)DROP DATABASE shop;ERROR: 55006: database "shop" is being accessed by other users
DETAIL: There is 1 other session using the database.WITH (FORCE) gives connected users no warning. Use it on test or disposable databases, or once you know who is connected.
\c shop
BEGIN;
SELECT now() IS NOT NULL AS in_tx;You are now connected to database "shop" as user "lab_a".
BEGIN
in_tx
-------
t
(1 row)DROP DATABASE shop WITH (FORCE);DROP DATABASEFAQ
How do I drop a PostgreSQL database that is being accessed by other users?
Either end the sessions first with SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'shop'; and then run DROP DATABASE, or run DROP DATABASE shop WITH (FORCE);. Both worked in our tests.
How do I see who is connected to a PostgreSQL database?
Query SELECT pid, datname, usename, state FROM pg_stat_activity WHERE datname = 'shop';. In our test it showed one lab_a session in state idle.
The other session is idle. Why does DROP DATABASE still fail?
DROP DATABASE only checks whether anyone is connected, not what they are doing. An idle session blocked it just like a session with an open transaction.
Does DROP DATABASE wait for the other session to disconnect?
Not in our tests. It failed with 55006 immediately in both cases, so retrying only helps once the other sessions are gone.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12