当前位置: 首页 > news >正文

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远程连接数据库超时的问题 / 张生荣

http://www.dtcms.com/a/353291.html

相关文章:

  • c#联合vision master 的基础教程
  • linux安装containerd
  • 如何使用 Xshell 8 连接到一台 CentOS 7 电脑(服务器)
  • MySQL 8 与 PostgreSQL 17 对比分析及迁移指南
  • 学习 Android (十七) 学习 OpenCV (二)
  • 【PHP】数学/数字处理相关函数汇总,持续更新中~
  • 极限RCE之三字节RCE
  • 嵌入式学习日记(35)TCP并发服务器构建
  • 指纹手机应用核心技术解析:从识别到智能交互
  • 搭建域服务器
  • 毕业项目推荐:28-基于yolov8/yolov5/yolo11的电塔危险物品检测识别系统(Python+卷积神经网络)
  • ChatGPT登录不进怎么办?
  • NumPy广播机制:高效数组运算的秘诀
  • 预测模型及超参数:2.传统机器学习:PLS及其改进
  • 守术,明法,悟道
  • 欧盟《人工智能法案》生效一年主要实施进展概览(二)
  • 如何借助文档控件 TX Text Control 轻松优化 PDF 文件大小?
  • 中科大携手智源发布 BGE-Reasoner:引领推理式信息检索新高度
  • AI数据治理:战略选择与伦理平衡
  • C6.4:晶体管模型
  • 语言切换时广播没有监听到语言变化
  • 从传统到创新:用报表插件重塑数据分析平台
  • OpenTelemetry 在 Spring Boot 项目中的3种集成方式
  • SciPy科学计算与应用:SciPy应用实战-数据分析与工程计算
  • SpringBoot集成 DeepSeek 对话补全功能
  • 安全建设之SLA指标(服务等级协议)
  • Linux基础优化(Ubuntu、Kylin)
  • k8s—部署discuz论坛和tomca商城
  • 轮转数组或者旋转数组-力扣189
  • 【开题答辩全过程】以基于Android的校园跳蚤市场交易系统的设计与实现为例,包含答辩的问题和答案