PostgreSQL Error 57P03: cannot connect now
PostgreSQL error 57P03 (cannot connect now) occurs when the database server is temporarily unable to accept new client connections. This typically happens when the server is in the middle of starting up, shutting down, or recovering from a crash or standby state. Unlike a permanent connection failure, this error is transient — meaning the server will eventually become available again.
Top 3 Causes
1. Server Is Still Starting Up
When PostgreSQL is booting (after pg_ctl start), it must initialize shared memory, replay WAL, and start background workers before accepting connections. If your application or script connects too early, you'll hit 57P03.
-- After connecting successfully, verify the server is fully up
SELECT pg_postmaster_start_time();
SELECT now() - pg_postmaster_start_time() AS server_uptime;
-- Check if the server is in recovery mode (standby or crash recovery)
SELECT pg_is_in_recovery();
-- Returns: false = Primary (ready), true = still recovering
Quick Fix: Use pg_isready before attempting to connect, and implement retry logic with exponential backoff in your application.
2. Standby Server Not Yet Ready (Hot Standby)
In a streaming replication setup, a standby server replays WAL logs before it can serve read queries. If hot_standby = off or recovery hasn't progressed far enough, any connection attempt returns 57P03.
-- Check replication lag and WAL replay status on the standby
SELECT
pg_last_wal_receive_lsn() AS received_lsn,
pg_last_wal_replay_lsn() AS replayed_lsn,
pg_last_xact_replay_timestamp() AS last_replayed_at,
now() - pg_last_xact_replay_timestamp() AS lag_seconds;
-- Verify hot_standby is enabled
SHOW hot_standby;
-- Check wal_level on primary (must be 'replica' or higher)
SHOW wal_level;
Quick Fix: Set hot_standby = on in postgresql.conf on all standby servers. Wait for pg_is_in_recovery() to return true and replication lag to decrease before routing traffic.
3. Server Is Shutting Down
During a planned shutdown (pg_ctl stop), PostgreSQL stops accepting new connections immediately. In smart mode, it waits for existing sessions to finish, which can extend the unavailability window significantly.
-- Before shutting down, check active connections
SELECT
count(*) AS total,
count(*) FILTER (WHERE state = 'active') AS active,
count(*) FILTER (WHERE state = 'idle in transaction') AS idle_in_tx
FROM pg_stat_activity
WHERE datname IS NOT NULL;
-- Gracefully terminate connections before shutdown
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = 'your_database'
AND pid <> pg_backend_pid();
-- Confirm no connections remain
SELECT count(*) FROM pg_stat_activity WHERE datname = 'your_database';
Quick Fix: Drain connections via your connection pooler (e.g., PgBouncer) before issuing a shutdown. Use pg_ctl stop -m fast for maintenance windows to reduce downtime.
Quick Fix Summary
-- 1. Check if server is in recovery
SELECT pg_is_in_recovery();
-- 2. Check server start time to estimate when it became available
SELECT pg_postmaster_start_time();
-- 3. Monitor active sessions before any maintenance
SELECT pid, usename, datname, state, wait_event
FROM pg_stat_activity
ORDER BY state;
-- 4. Reload config without restart (when possible)
SELECT pg_reload_conf();
-- 5. Confirm parameter context (postmaster = needs restart, sighup = reload only)
SELECT name, setting, context
FROM pg_settings
WHERE name IN ('hot_standby', 'wal_level', 'max_connections');
Prevention Tips
1. Implement Retry Logic with a Connection Pooler
Never assume the database is immediately available after a restart. Use PgBouncer or Pgpool-II in front of PostgreSQL so that reconnection logic is handled at the pooler level. In your application code, catch error code 57P03 specifically and retry with exponential backoff (e.g., 1s → 2s → 4s → 8s).
2. Use pg_isready in Health Checks and CI/CD Pipelines
Integrate pg_isready -h <host> -p 5432 into your deployment scripts and load balancer health checks. This prevents traffic from being routed to a server that is still initializing. Also ensure hot_standby = on and wal_level = replica are set by default on all nodes to minimize 57P03 exposure during failover events.
Related Errors
| Code | Name | Description |
|---|---|---|
| 57P01 | admin_shutdown | Server was shut down by an administrator |
| 57P02 | crash_shutdown | Server crashed and is recovering |
| 53300 | too_many_connections | Connection limit (max_connections) exceeded |
| 08006 | connection_failure | Physical/network-level connection failure |
📖 Want a more detailed guide?
Check out the full in-depth version (Korean) on oraerror.com — includes detailed analysis, additional SQL examples, and prevention tips.
Top comments (0)