How to fix Could not receive data from client: Connection reset by peer in PostgreSQL
This error occurs when a PostgreSQL client disconnects abruptly without properly closing the connection, often due to network issues, timeout settings, or firewall interference. Fix it by configuring TCP keepalive settings, implementing connection pooling, and ensuring proper client-side connection handling.
Quick fix
Fixes Could not receive data from client: Connection reset by peer
tail -f /var/log/postgresql/postgresql.log- Check PostgreSQL logs for patterns
- Configure TCP keepalive settings
- Use connection pooling (PgBouncer)
- Set client-side connection timeout
tail -f /var/log/postgresql/postgresql.logCould not receive data from client: Connection reset by peer
What this error means
The PostgreSQL server attempted to read data from a connected client but found that the client had already closed the TCP connection abruptly. This is not a server failure—it indicates the client-side or network layer terminated the connection ungracefully. The server logs this as a warning because it was waiting for the client to send the next SQL statement (visible as "ClientRead" in pg_stat_activity) when the connection vanished.
Typical symptoms
- PostgreSQL server logs show "could not receive data from client: Connection reset by peer"
- Application connections drop unexpectedly after a period of inactivity
- Intermittent connection failures that resolve on retry
- Long-running queries or idle connections are terminated prematurely
- Multiple backend processes left in abandoned state in pg_stat_activity
Common causes
- Client application crashes or exits without properly closing the database connection
- Network interruption between client and server (router, firewall, or ISP issue)
- Firewall or load balancer idle timeout terminating long-lived connections after 5-10 minutes
- VIP health checks or network monitoring tools probing port 5432
- Client-side connection pool timeout being shorter than server idle timeout
- TCP keepalive not configured, leaving the server unaware of dead connections for hours
How to fix "Could not receive data from client: Connection reset by peer"
Review the PostgreSQL server logs to identify when and why connections are being reset:
tail -f /var/log/postgresql/postgresql.logLook for the frequency and timing of "Connection reset by peer" messages. If they appear every 5 minutes, it's likely a firewall or load balancer timeout.
PostgreSQL has three TCP keepalive parameters that detect broken connections. Add these to your postgresql.conf file:
tcp_keepalives_idle = 240 # 4 minutes
tcp_keepalives_interval = 30 # 30 seconds
tcp_keepalives_count = 5 # 5 probesThese settings cause the server to probe idle connections every 4 minutes. If the client doesn't respond after 5 failed probes (150 seconds later), the connection is considered dead.
Reload the configuration:
sudo systemctl reload postgresqlInstall and configure PgBouncer to manage database connections efficiently:
sudo apt-get install pgbouncerEdit /etc/pgbouncer/pgbouncer.ini:
[databases]
mydb = host=localhost port=5432 dbname=mydb
[pgbouncer]
listen_port = 6432
pool_mode = transaction
max_client_conn = 1000
default_pool_size = 25
reserve_pool_size = 5Then update your application to connect to localhost:6432 instead of the PostgreSQL port directly. PgBouncer will manage keepalive and connection reuse automatically.
Configure your database driver or ORM to use a reasonable connection timeout. For example, with psycopg2 in Python:
import psycopg2
conn = psycopg2.connect(
host="localhost",
database="mydb",
user="user",
password="password",
connect_timeout=5
)For Node.js with pg:
const { Client } = require("pg");
const client = new Client({
host: "localhost",
database: "mydb",
user: "user",
password: "password",
connectionTimeoutMillis: 5000,
idleTimeoutMillis: 30000,
});Without a timeout, your app may hang for 30+ seconds on network failures.
Work with your network/DevOps team to verify:
1. Firewall idle timeout is longer than your TCP keepalive interval (ideally > 10 minutes)
2. Load balancer or proxy is not aggressively closing idle connections
3. VIP health checks are not probing port 5432 every few seconds
If a VIP monitor is checking the port, coordinate with network ops to use a separate health check port or implement a dedicated health check endpoint.
For PostgreSQL 9.6+, set a server-side idle timeout to clean up abandoned sessions:
ALTER SYSTEM SET idle_session_timeout = '15min';Reload:
sudo systemctl reload postgresqlThis will automatically close sessions idle for more than 15 minutes, preventing resource exhaustion from abandoned connections.
Advanced notes
PostgreSQL v14+ introduced client_connection_check_interval (Linux only), which allows the server to poll client sockets regularly to detect closed connections immediately rather than waiting for the next read attempt. Also note that the "Connection reset by peer" error is NOT the same as "Connection timed out"—the former means the peer actively closed the connection, while the latter means no response was received to a connection attempt. You can track abandoned connections in PostgreSQL v14+ via the pg_stat_database.sessions_abandoned metric.
Frequently asked questions
- What does "Could not receive data from client: Connection reset by peer" mean?
- The PostgreSQL server attempted to read data from a connected client but found that the client had already closed the TCP connection abruptly. This is not a server failure—it indicates the client-side or network layer terminated the connection ungracefully. The server logs this as a warning because it was waiting for the client to send the next SQL statement (visible as "ClientRead" in pg_stat_activity) when the connection vanished.
- How do I fix "Could not receive data from client: Connection reset by peer"?
- This error occurs when a PostgreSQL client disconnects abruptly without properly closing the connection, often due to network issues, timeout settings, or firewall interference. Fix it by configuring TCP keepalive settings, implementing connection pooling, and ensuring proper client-side connection handling.
- What causes "Could not receive data from client: Connection reset by peer"?
- Client application crashes or exits without properly closing the database connection Network interruption between client and server (router, firewall, or ISP issue) Firewall or load balancer idle timeout terminating long-lived connections after 5-10 minutes
- Is the "Could not receive data from client: Connection reset by peer" error serious?
- It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- 📚PostgreSQL Official Documentation
- 📖PostgreSQL Docs - Client Connection Defaults(Official Docs)
- 📖PostgreSQL Docs - Error Codes(Official Docs)