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.
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.
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.
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.
ERROR: syntax error at end of input
Syntax error at end of input in PostgreSQL
Bind message supplies N parameters but prepared statement requires M
Bind message supplies N parameters but prepared statement requires M in PostgreSQL
Multidimensional arrays must have sub-arrays with matching dimensions
Multidimensional arrays must have sub-arrays with matching dimensions
ERROR: value too long for type character varying
Value too long for type character varying
insufficient columns in unique constraint for partition key
How to fix "insufficient columns in unique constraint for partition key" in PostgreSQL