PostgreSQL 55006: object in use
The object can't be changed while others use it, typically DROP DATABASE, ALTER DATABASE RENAME or a template copy while sessions are connected.
- SQLSTATE
55006- Name
object_in_use- Category
- Object Not In Prerequisite State
What it means
Some operations need exclusive use of an object. Dropping or renaming a database, or creating a database from a template, requires that no other session is connected to it, and PostgreSQL reports 55006 (object_in_use) with the number of other sessions. The same code appears for tables in use by the current session, for example cannot ALTER TABLE "t" because it is being used by active queries in this session.
Your own connection counts too: you can't drop the database you are connected to.
Common causes
- Application connections, connection pools, monitoring tools or GUI clients are still connected to the database.
- You are connected to the database you're trying to drop.
- Creating a database from a template database that has open connections.
How to fix it
-
Connect to another database (such as
postgres), then see who is connected.SELECT pid, usename, application_name, client_addr FROM pg_stat_activity WHERE datname = 'shop';
-
On PostgreSQL 13 and later, drop the database and terminate its sessions in one step.
DROP DATABASE shop WITH (FORCE);
-
On older versions, block new connections, terminate existing ones, then drop.
ALTER DATABASE shop ALLOW_CONNECTIONS false; SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'shop' AND pid <> pg_backend_pid(); DROP DATABASE shop;
Related errors
57P01terminating connection due to administrator55P03lock not available3D000database does not exist3702SQL Server: Cannot drop database, currently in use3101SQL Server: Exclusive access could not be obtained
More object not in prerequisite state errors
The code and message text are from the PostgreSQL documentation, Appendix A: PostgreSQL Error Codes. Browse the full PostgreSQL error list or search every database error.
Work on PostgreSQL in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for PostgreSQL and the other databases you use.