55006
PostgreSQLERRORNotableObject Not in Prerequisite StateHIGH confidence

object in use

What this means

SQLSTATE 55006 is raised when an operation cannot proceed because the target object is currently in use by another session — for example, attempting to DROP a database while active connections exist, or reconfiguring an object that is currently being accessed.

Why it happens
  1. 1DROP DATABASE while client sessions are connected to it
  2. 2ALTER TABLESPACE or similar DDL on an object currently in use by active queries
How to reproduce

DROP DATABASE with active connections.

trigger — this will error
trigger — this will error
DROP DATABASE myapp; -- active sessions are connected

expected output

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

Fix 1

Terminate active connections before dropping the database

WHEN When dropping a database with active sessions.

Terminate active connections before dropping the database
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = 'myapp' AND pid <> pg_backend_pid();

DROP DATABASE myapp;

Why this works

pg_terminate_backend closes all other connections to the database, clearing the way for DROP DATABASE.

Fix 2

Use DROP DATABASE WITH (FORCE) in Postgres 13+

WHEN When immediate forced deletion is needed.

Use DROP DATABASE WITH (FORCE) in Postgres 13+
DROP DATABASE myapp WITH (FORCE);

Why this works

The FORCE option terminates existing connections before dropping the database in one step.

What not to do

Use pg_terminate_backend on the wrong database in production

Terminating connections on the wrong database causes an outage for legitimate users.

Version notes
Postgres 13+

DROP DATABASE WITH (FORCE) option added in Postgres 13.

Sources
Official documentation ↗

Class 55 — Object Not in Prerequisite State

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All PostgreSQL errors