PostgreSQL 17.12Lab-verified · 2026-11-30

PostgreSQL 55006: database "shop" is being accessed by other users

DROP DATABASE was refused because another session was still connected to that database.

PostgreSQL 17.12
55006: database "shop" is being accessed by other users

Jump to a cause

  1. 01Another user is still connected to the database (even an idle one)
  2. 02A session with an open transaction is connected; drop in one step with WITH (FORCE)

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
Setuprun as postgres
DROP DATABASE IF EXISTS shop WITH (FORCE);
CREATE DATABASE shop;
OUTPUT
NOTICE:  00000: database "shop" does not exist, skipping
DROP DATABASE
CREATE DATABASE
Reproduce3 sessions open at the same time (A, C, B), run in this order
Session Arun as lab_a
\c shop
SELECT current_database();
OUTPUT
You are now connected to database "shop" as user "lab_a".
 current_database
------------------
 shop
(1 row)
Session C · Checkrun as postgres
SELECT pid, datname, usename, state FROM pg_stat_activity WHERE datname = 'shop';
OUTPUT
 pid  | datname | usename | state
------+---------+---------+-------
 4325 | shop    | lab_a   | idle
(1 row)
Session Brun as postgres
DROP DATABASE shop;
OUTPUT
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.

After the fix2 sessions open at the same time (A, B), run in this order
Session Arun as lab_a
\c shop
SELECT current_database();
OUTPUT
You are now connected to database "shop" as user "lab_a".
 current_database
------------------
 shop
(1 row)
Session B · Fixrun as postgres
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'shop';
DROP DATABASE shop;
OUTPUT
 pg_terminate_backend
----------------------
 t
(1 row)

DROP DATABASE

02A 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
Setuprun as postgres
DROP DATABASE IF EXISTS shop WITH (FORCE);
CREATE DATABASE shop;
OUTPUT
NOTICE:  00000: database "shop" does not exist, skipping
DROP DATABASE
CREATE DATABASE
Reproduce2 sessions open at the same time (A, B), run in this order
Session Arun as lab_a
\c shop
BEGIN;
SELECT now() IS NOT NULL AS in_tx;
OUTPUT
You are now connected to database "shop" as user "lab_a".
BEGIN
 in_tx
-------
 t
(1 row)
Session Brun as postgres
DROP DATABASE shop;
OUTPUT
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.

After the fix2 sessions open at the same time (A, B), run in this order
Session Arun as lab_a
\c shop
BEGIN;
SELECT now() IS NOT NULL AS in_tx;
OUTPUT
You are now connected to database "shop" as user "lab_a".
BEGIN
 in_tx
-------
 t
(1 row)
Session B · Fixrun as postgres
DROP DATABASE shop WITH (FORCE);
OUTPUT
DROP DATABASE

FAQ

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