postgreSql远程连接数据库总是超时断开?
问题:postgresql经常遇到连接中断的情况,程序几分钟就会断一次很难受。
pg的日志大量报错:
2025-08-27 11:05:43.967 CST [26462] LOG: could not receive data from client: Connection reset by peer
2025-08-27 11:05:43.967 CST [26259] LOG: could not receive data from client: Connection reset by peer
2025-08-27 11:05:43.967 CST [26204] LOG: could not receive data from client: Connection reset by peer
2025-08-27 11:05:43.968 CST [26225] LOG: could not receive data from client: Connection reset by peer
2025-08-27 11:12:15.124 CST [27046] LOG: could not receive data from client: Connection timed out
2025-08-27 11:23:24.664 CST [32744] LOG: could not receive data from client: Connection timed out
2025-08-27 11:30:10.448 CST [28921] LOG: could not receive data from client: Connection reset by peer
2025-08-27 12:19:02.304 CST [26873] LOG: could not receive data from client: Connection timed out
2025-08-27 12:19:02.552 CST [26916] LOG: could not receive data from client: Connection timed out
2025-08-27 13:34:00.292 CST [31451] LOG: received immediate shutdown request
2025-08-27 13:34:00.293 CST [10338] WARNING: terminating connection because of crash of another server process
2025-08-27 13:34:00.293 CST [10338] DETAIL: The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly
corrupted shared memory.
2025-08-27 13:34:00.293 CST [10338] HINT: In a moment you should be able to reconnect to the database and repeat your command.
2025-08-27 13:34:00.293 CST [9922] WARNING: terminating connection because of crash of another server process
2025-08-27 13:34:00.293 CST [9922] DETAIL: The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly c
orrupted shared memory.
2025-08-27 13:34:00.293 CST [9922] HINT: In a moment you should be able to reconnect to the database and repeat your command.
2025-08-27 13:34:00.293 CST [10153] WARNING: terminating connection because of crash of another server process
2025-08-27 13:34:00.294 CST [14697] HINT: In a moment you should be able to reconnect to the database and repeat your command.
2025-08-27 13:34:00.294 CST [27461] WARNING: terminating connection because of crash of another server process
2025-08-27 13:34:00.294 CST [27461] DETAIL: The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory.
尝试修改swap等操作:
[root@apm ~]# swapoff -a
[root@apm ~]# swapon -a
[root@apm ~]# cat /etc/sysctl.conf
vm.max_map_count=262144
vm.swappiness = 1
[root@apm ~]# free -h
total used free shared buff/cache available
Mem: 31G 22G 194M 2.1G 8.3G 6.0G
Swap: 2.0G 0B 2.0G
处理办法:
1.系统参数修改:
[root@apm ~]$ sysctl -a|grep keepalive
net.ipv4.tcp_keepalive_intvl = 75
net.ipv4.tcp_keepalive_probes = 9
net.ipv4.tcp_keepalive_time = 7200
改成:
net.ipv4.tcp_keepalive_intvl = 10
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_time = 60
sysctl -p生效。
2.数据库参数修改:
[postgres@apm ~]$ psql
psql (12.2)
Type "help" for help.
postgres=# select name,setting,reset_val from pg_settings where name ~ 'tcp';
name | setting | reset_val
-------------------------+---------+-----------
tcp_keepalives_count | 0 | 0
tcp_keepalives_idle | 0 | 1
tcp_keepalives_interval | 0 | 1
tcp_user_timeout | 0 | 0
(4 rows)
postgres=# alter system set tcp_keepalives_count=3;
ALTER SYSTEM
postgres=# alter system set tcp_keepalives_idle=300;
ALTER SYSTEM
postgres=# alter system set tcp_keepalives_interval=30;
ALTER SYSTEM
postgres=# select name,setting,reset_val from pg_settings where name ~ 'tcp';
name | setting | reset_val
-------------------------+---------+-----------
tcp_keepalives_count | 0 | 0
tcp_keepalives_idle | 0 | 1
tcp_keepalives_interval | 0 | 1
tcp_user_timeout | 0 | 0
(4 rows)
[postgres@apm ~]$ pwd
/home/postgres
[postgres@apm ~]$ cd ../postgresql/data
[postgres@apm data]$ more postgresql.auto.conf
# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command.
max_connections = '1000'
search_path = '"$user", gkldb, public, postgres, mt, csdl'
tcp_keepalives_count = '3'
tcp_keepalives_idle = '300'
tcp_keepalives_interval = '30'
重启数据库生效:su - postgres -c '/home/postgresql/bin/pg_ctl -D /home/postgresql/data -l /home/postgresql/data/logfile start'
[postgres@apm data]$ psql
psql (12.2)
Type "help" for help.
postgres=# select name,setting,reset_val from pg_settings where name ~ 'tcp';
name | setting | reset_val
-------------------------+---------+-----------
tcp_keepalives_count | 0 | 3
tcp_keepalives_idle | 0 | 300
tcp_keepalives_interval | 0 | 30
tcp_user_timeout | 0 | 0
(4 rows)
相关参考:
postgresql tcp 连接超时问题 - 简书
postgresql经常出现连接一会后服务器拒绝连接_postgresql连接一会就超时-CSDN博客
解决postgreSql远程连接数据库超时的问题 / 张生荣