57P01
PostgreSQLFATALNotableOperator InterventionHIGH confidence

terminating connection due to administrator command

What this means

An administrator called pg_terminate_backend() or issued a server shutdown, causing the backend to terminate the client connection. The current transaction is rolled back and the connection is closed.

Why it happens
  1. 1pg_terminate_backend(pid) was called explicitly by a superuser
  2. 2The Postgres server is shutting down (SIGTERM or pg_ctl stop)
  3. 3idle_in_transaction_session_timeout fired and was configured to terminate rather than just cancel
  4. 4A connection pool manager forcibly closed a backend that was idle too long
How to reproduce

An administrator terminates a backend session from another connection.

trigger — this will error
trigger — this will error
-- From an admin session:
SELECT pg_terminate_backend(12345); -- sends SIGTERM to backend 12345

-- The victim session receives:
-- FATAL: terminating connection due to administrator command

expected output

FATAL:  terminating connection due to administrator command
server closed the connection unexpectedly

Fix

Reconnect and retry the operation

WHEN When the termination was unintentional or a one-time event.

Reconnect and retry the operation
-- Application should catch SQLSTATE 57P01 and reconnect.
-- Before terminating active sessions, prefer pg_cancel_backend() for non-destructive cancellation:
SELECT pg_cancel_backend(12345); -- cancels current query, leaves connection open

Why this works

pg_cancel_backend() sends SIGINT to the backend, which cancels the current query and returns the connection to idle state. pg_terminate_backend() sends SIGTERM, which closes the connection entirely. Using cancel instead of terminate is less disruptive.

What not to do

Call pg_terminate_backend on all idle connections in a loop as a routine cleanup

Disrupts legitimate idle-in-transaction sessions and forces unnecessary reconnects; use idle_in_transaction_session_timeout instead for automatic cleanup.

Sources
Official documentation ↗

src/backend/tcop/postgres.c — ProcessInterrupts()

Server Signalling Functions

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

← All PostgreSQL errors