PostgreSQL: server closed the connection unexpectedly
server closed the connection unexpectedlyThe connection dropped mid-request. The backend may have crashed (often the out-of-memory killer), been terminated by an admin or restart, or the socket was severed by a timeout or proxy. Check the server log for the real cause and add reconnect logic.
What this error means
This is a client-side message: the socket to the backend closed while a request was in flight, and the client cannot tell why, only that the server went away.
The true cause is in the server log: a crash (PANIC or a signal), an admin restart, an out-of-memory kill, or a proxy or timeout dropping an idle or long-running connection.
How to fix it
The backend crashed or was OOM-killed
Check the server log around the disconnect. If it was killed for memory, lower work_mem or add RAM.
-- look for PANIC, terminating, or a signal 9 in the Postgres logA restart or failover terminated it
Expected during maintenance. Add reconnect and retry logic in your client or pool.
An idle or long connection was dropped
Set TCP keepalives, shorten idle_in_transaction_session_timeout, and check any proxy or load-balancer timeouts.
How to avoid it next time
- Add reconnect and retry logic so restarts and failovers are transparent.
- Right-size memory and set sane keepalive and timeout values.
This is a client-side libpq message with no server SQLSTATE. 08006 (connection_failure) is the nearest class. A clean admin termination instead arrives as 57P01 (admin_shutdown).
Frequently asked questions
How do I find why it closed?
The client cannot say. Read the Postgres server log around the timestamp; it records crashes, restarts, and terminations.
Is this a network or a server problem?
Either. A crash or OOM kill is server-side; a dropped idle socket is often a proxy or network timeout. The server log distinguishes them.
Stop struggling with SQL
Use FluentDB, the AI database client for macOS.
Related errors
Part of the PostgreSQL error reference. Last reviewed 2026-07-31.