Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL Error 57P03 Cannot Connect Now: How to Fix

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

PostgreSQL error 57P03 means the server is not accepting new connections, usually because it’s in recovery, shutting down, or exhausted its connection limit. This isn’t a network issue—it’s a server state problem. The fix requires checking the server’s status, adjusting connection limits, or restarting the service properly.

What You'll Need

  • Access to the machine running PostgreSQL (local or remote via SSH)
  • Administrative privileges (sudo or root access)
  • Terminal or command-line interface
  • Access to PostgreSQL logs (typically in /var/log/postgresql/ or /var/lib/postgresql/data/log/)

Step-by-Step

  1. Check if PostgreSQL is running by typing sudo systemctl status postgresql or sudo systemctl status postgresql@-main (replace with your version, e.g., 15). If it shows "active (running)", proceed to step 3. If it shows "inactive (dead)" or "failed", go to step 2.
  2. If PostgreSQL is not running, start it with sudo systemctl start postgresql. Wait 10–15 seconds, then check status again. If it fails to start, view the logs with sudo tail -n 50 /var/log/postgresql/postgresql--main.log to identify startup errors like corrupted WAL files or permission issues.
  3. If PostgreSQL is running but still rejecting connections, check the maximum number of connections allowed. Connect as a superuser (e.g., via sudo -u postgres psql) and run: SHOW max_connections;. Compare this to current connections: SELECT count(*) FROM pg_stat_activity;. If the count is near or exceeds max_connections, the server is at capacity.
  4. To free up connections, identify idle or stuck processes. Run: SELECT pid, usename, application_name, state, query_start FROM pg_stat_activity WHERE state = 'idle' OR state = 'idle in transaction';. If you see long-running idle transactions, terminate them with SELECT pg_terminate_backend(pid); for each unwanted PID. Be cautious—only kill processes you are certain are safe to terminate.
  5. If connections are not the issue, check if the server is in recovery mode. Run: SELECT pg_is_in_recovery();. If it returns "true", the server is a standby replica waiting for WAL data. Wait for replication to catch up or check the replication source. If this is a primary server and it shows recovery mode, there may be a crash or restore operation in progress.
  6. Review PostgreSQL configuration. Open /etc/postgresql//main/postgresql.conf (adjust path for your OS/version). Verify listen_addresses includes the correct IP (e.g., '*' or 'localhost') and that max_connections is set appropriately (default is often 100; increase to 200 or 300 if needed). After editing, restart PostgreSQL with sudo systemctl restart postgresql.
  7. Check for disk space issues. Run df -h. If the partition holding PostgreSQL data (often /var/lib/postgresql/) is over 90% full, the server will refuse connections. Clean up old logs, archive WAL files, or expand storage.
  8. If the server was shut down improperly (power loss, kill -9), it may need recovery. Look for "database system was not properly shut down" in the logs. PostgreSQL usually recovers automatically on restart. If it hangs, try sudo systemctl stop postgresql, then sudo -u postgres pg_resetwal /var/lib/postgresql//main (only as a last resort—this can cause data loss).

Common Mistakes

  • Assuming it’s a network or firewall issue—error 57P03 is server state, not connectivity. Test with psql -h localhost -U username database locally before checking network rules.
  • Restarting PostgreSQL without checking logs first—this hides the root cause. Always inspect logs after a failed start.
  • Increasing max_connections without monitoring actual usage—this can exhaust system memory and cause crashes.
  • Running pg_resetwal without backup or understanding the risk—it irreversibly clears WAL logs and may corrupt data if used incorrectly.
  • Killing all backend processes blindly—some are essential system processes. Only terminate idle or stuck user sessions.

Tips

  • Set up monitoring: Use pg_stat_activity to alert when connections exceed 80% of max_connections.
  • Use connection pooling (e.g., pgBouncer) in production to reduce direct connections to PostgreSQL.
  • Configure autovacuum properly to prevent transaction ID wraparound, which can trigger recovery mode.
  • Enable logging of slow queries and idle transactions to catch long-running sessions before they block the system.
  • Always test configuration changes on a staging server first.
  • Keep at least 20% free disk space on the data volume to avoid unexpected connection rejection.

Troubleshooting

If the above steps don’t resolve the issue, proceed methodically:

  1. Check if another PostgreSQL instance is running on the same port. Run sudo lsof -i :5432. If multiple processes appear, kill the extra one with sudo kill -9 .
  2. Verify file permissions in the data directory. Run ls -la /var/lib/postgresql//main/. All files should be owned by the postgres user. Fix with sudo chown -R postgres:postgres /var/lib/postgresql//main/.
  3. If the server started but still rejects connections, check pg_hba.conf. Ensure it allows your client’s host and authentication method (e.g., host all all 127.0.0.1/32 md5).
  4. Try connecting via Unix socket instead of TCP: psql -U username database (no host specified). If this works, the issue is TCP-specific (e.g., bind address misconfiguration).
  5. If you’re on a cloud server (AWS RDS, Azure Database, etc.), check the service’s dashboard for maintenance windows or connection limits. Some managed services auto-restrict connections during high load.
  6. If all else fails, back up critical databases with pg_dump (if you can still connect for read-only queries), then restart the server with a clean data directory after backing up the old one. Reinstall PostgreSQL only if the data directory is irrecoverable.

Frequently Asked Questions

Why does PostgreSQL reject connections even when it seems to be running?

It may be in recovery mode, at max_connections, or waiting for disk I/O (e.g., due to low storage or slow I/O). The server intentionally blocks new connections to prevent data corruption during unstable states.

Can I increase max_connections without restarting PostgreSQL?

No. You must edit postgresql.conf and restart the service for changes to max_connections to take effect. Use SELECT set_config('max_connections', '300', false); for runtime changes only on some versions, but this is unreliable and not recommended.

What does pg_is_in_recovery() returning true mean?

It means the server is operating as a standby replica, reading WAL files from a primary. If this is your primary server, it likely crashed and is recovering. Wait or check for corruption.

How do I know if disk space is causing this error?

Run df -h and look at the partition holding PostgreSQL data. If it’s over 95% full, the server will refuse new connections to avoid data loss. Clear old logs or archive WAL files.

Is it safe to use pg_resetwal to fix this error?

Only as a last resort. It removes WAL logs and may cause data loss. Use only if the server won’t start and you have no other option. Always backup first.

Why do connections pile up and never close?

Applications often fail to close database connections properly. Use connection pooling, set idle_timeout in your app, and monitor pg_stat_activity regularly to kill stale sessions.