dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Home/Error Library/postgresql-database-being-accessed
PostgreSQL
PG 13 - 17Session & Lock
5 min read
ERROR: database "test_db" is being accessed by other users

PostgreSQL "database is being accessed by other users": Why Drop Database Is Blocked?

Safely terminating active sessions before DROP DATABASE, ALLOW_CONNECTIONS isolation, prepared transactions, and replication slot blockers.

Reviewed by:Senior Database Architect & SRE (dataiotech Labs)
Tested Versions: PostgreSQL 13 / 14 / 15 / 16 / 17
First 60 Seconds Emergency Protocol:

The error string is a symptom, not the root cause. Do not restart the server blindly. Run the diagnostic queries below to pinpoint the actual failure mode.

▸Symptoms

plaintext
ERROR: database "test_db" is being accessed by other users DETAIL: There are 3 other sessions using the database.

▸Resolution Sequence

  1. 1Disallow Incoming Connections (PG 13+):
sql
ALTER DATABASE test_db WITH ALLOW_CONNECTIONS false;
  1. 1Terminate Active Sessions:
sql
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'test_db' AND pid <> pg_backend_pid();
  1. 1Inspect Invisible Blockers:
  • •Prepared transactions: SELECT gid, database FROM pg_prepared_xacts; (ROLLBACK PREPARED 'gid';)
  • •Replication slots: SELECT slot_name FROM pg_replication_slots WHERE database = 'test_db'; (SELECT pg_drop_replication_slot('...');)
  1. 1Drop Database:
sql
DROP DATABASE test_db;

Other Database Error Triage Playbooks

Other battle-tested diagnostic guides for high-severity database errors.

All Errors (20)