PostgreSQL error 57P03 occurs when the server cannot accept connections because it is starting up, shutting down, or in recovery mode. This is typically temporary and resolves once the server completes its startup or recovery process.
The "Cannot connect now" error (SQLSTATE 57P03) indicates that the PostgreSQL server is not ready to accept client connections. This happens during critical server states like startup, shutdown, or database recovery. The server actively rejects new connections to prevent data corruption or inconsistent state. This error is distinct from "too many connections" (53300) and is a temporary condition that should resolve automatically.
On a normal restart, the server typically accepts connections within a few seconds. For crash recovery with large transaction logs, this can take minutes depending on I/O speed and database size. Monitor PostgreSQL logs to confirm recovery is finished:
tail -f /var/log/postgresql/postgresql.logLook for the message "database system is ready to accept connections" which indicates the server is ready.
Configure your application or connection pool to retry connections with increasing delays rather than failing immediately. Most modern drivers support this:
Node.js with pg:
const client = new Client({ database: 'mydb' });
await client.connect().catch(err => {
if (err.code === '57P03') {
console.log('Server starting, retrying...');
setTimeout(() => client.connect(), 2000);
}
});Python with psycopg2:
import time
import psycopg2
max_retries = 10
for i in range(max_retries):
try:
conn = psycopg2.connect("dbname=mydb user=postgres")
break
except psycopg2.OperationalError as e:
if '57P03' in str(e):
wait_time = 2 ** i # Exponential backoff
time.sleep(wait_time)On replica servers in replication setups, enable hot standby to allow read-only connections during recovery:
sudo -u postgres psql -c "ALTER SYSTEM SET hot_standby = on;"
sudo systemctl restart postgresqlVerify the setting:
sudo -u postgres psql -c "SHOW hot_standby;"This allows applications to connect with application_name parameters for read-only queries while the replica recovers.
Use pg_isready utility to verify server state without consuming a connection slot:
pg_isready -h localhost -p 5432 -U postgresOutput codes:
- 0: Server is accepting connections
- 1: Server is rejecting connections
- 2: Cannot establish connection
- 3: No attempt (invalid arguments)
In Kubernetes or container orchestration, use this in startup probes:
livenessProbe:
exec:
command: ["pg_isready", "-h", "localhost"]
initialDelaySeconds: 30
periodSeconds: 5If crash recovery takes an unusually long time, optimize these settings in postgresql.conf:
sudo -u postgres psql -c "ALTER SYSTEM SET checkpoint_completion_target = 0.9;"
sudo -u postgres psql -c "ALTER SYSTEM SET max_wal_size = 4GB;"
sudo systemctl restart postgresqlThese settings:
- checkpoint_completion_target: Increases time between checkpoints, reducing recovery time
- max_wal_size: Allows more WAL accumulation before checkpoint, reducing checkpoint frequency
Restart PostgreSQL to apply changes. Monitor recovery progress in logs.
Error 57P03 is related to but distinct from other connection errors: SQLSTATE 57P02 (crash_shutdown) indicates the server crashed and is recovering; SQLSTATE 53300 (too_many_connections) indicates resource exhaustion rather than startup state; SQLSTATE 08001 (cannot_connect) indicates networking issues. For replicas, crash recovery duration depends on WAL volume and I/O performance. In cloud environments, pay attention to maintenance windows that trigger restarts. Some managed database services (RDS, Cloud SQL) may show extended 57P03 periods during failovers.
insufficient columns in unique constraint for partition key
How to fix "insufficient columns in unique constraint for partition key" in PostgreSQL
ERROR 42501: must be owner of table
How to fix "must be owner of table" in PostgreSQL
trigger cannot change partition destination
How to fix "Trigger cannot change partition destination" in PostgreSQL
SSL error: certificate does not match host name
SSL error: certificate does not match host name in PostgreSQL
No SSL connection
No SSL connection to PostgreSQL