connection closed 🐘 PostgreSQL

server closed the connection unexpectedly — This probably means the server terminated abnormally before or while processing the request.

The connection to PostgreSQL dropped — the server restarted/crashed, a proxy/firewall killed an idle connection, or the backend was terminated.

Seen on: PostgreSQL

Meaning

Check the PostgreSQL log at that time: OOM kills, crashes and restarts appear there. Otherwise idle connections were cut by NAT/load balancers.

Common causes

  • Server restart or crash (OOM)
  • Idle connection killed by firewall/NAT/proxy
  • pg_terminate_backend by an admin
  • Network interruption

⚡ Quick fix

  1. Check the PostgreSQL log
  2. Enable TCP keepalives / pool health checks (pre-ping)
  3. Reconnect and retry idempotent work

Detailed fix by platform

PostgreSQL

  1. keepalives_idle=60 in the connection string, or pool_pre_ping=True in SQLAlchemy

How to diagnose

  1. Server log — Restart/crash at that time?
  2. Idle — Was the connection idle long?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.