惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News | PayPal Newsroom
H
Hackread – Cybersecurity News, Data Breaches, AI and More
雷峰网
雷峰网
NISL@THU
NISL@THU
V
Vulnerabilities – Threatpost
P
Privacy International News Feed
博客园_首页
P
Privacy & Cybersecurity Law Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
Know Your Adversary
Know Your Adversary
小众软件
小众软件
人人都是产品经理
人人都是产品经理
WordPress大学
WordPress大学
博客园 - 聂微东
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Tor Project blog
C
Cybersecurity and Infrastructure Security Agency CISA
V
V2EX
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
J
Java Code Geeks
M
MIT News - Artificial intelligence
PCI Perspectives
PCI Perspectives
T
The Blog of Author Tim Ferriss
美团技术团队
Jina AI
Jina AI
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
Webroot Blog
Webroot Blog
V2EX - 技术
V2EX - 技术
爱范儿
爱范儿
S
Securelist
MyScale Blog
MyScale Blog
B
Blog
AI
AI
L
LINUX DO - 热门话题
Security Archives - TechRepublic
Security Archives - TechRepublic
The Register - Security
The Register - Security
I
Intezer
有赞技术团队
有赞技术团队
Google Online Security Blog
Google Online Security Blog
云风的 BLOG
云风的 BLOG
H
Help Net Security
D
DataBreaches.Net
K
Kaspersky official blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
The Last Watchdog
The Last Watchdog
Attack and Defense Labs
Attack and Defense Labs
酷 壳 – CoolShell
酷 壳 – CoolShell

PostgreSQL

五年过去了,结论有什么变化吗? 适配信创数据库的人的精神状态 be like - V2EX 用了十几年的 MySQL 了,突然发现 PostgreSQL 可能更加适合我,大家怎么看? - V2EX postgresql 多进程改多线程 - V2EX 二键安装一个 postgresql 服务 - V2EX 通过 pg_roaringbitmap 优化标签查询的话,有没有必要做高频标签映射和结合普通复合索引? - V2EX PostgreSQL + TimescaleDB 方案不按主键索引走,有什么办法吗? - V2EX 看看大家现在用 pg 的最佳实践是怎么样的 - V2EX 用 PostgreSQL 存图片等 binary 有什么坑吗 - V2EX [求助] 在哪个网址能够下载 PostgresSQL 9.3.0 版本的 Windows 二进制安装包?官方/ftp/old/binary 里没有。 - V2EX [请教]pgvector 的性能问题 - V2EX 多笔记本离线 PostgreSQL 数据库同步问题: UUID vs 自增主键 - V2EX PostgreSQL 通过分 Database 做多租户可行吗? PostgreSQL 多租户的正确姿势 - V2EX Postgresql 放 K8S 里,大家目前都用的什么方案呢? - V2EX 2025 年 postgresql 有什么优秀的备份解决方案 - V2EX 有大神研究过 pg_resetwal 的原理吗,为什么会丢 user 表的数据 - V2EX 按照 stackoverflow 教程执行 pg_resetwal, postgresql 的数据库被清空了 - V2EX 求国内高性价比的 postgresql 服务,纳米项目 - V2EX PostgreSQL 18 JSONB 增加能取代 MongoDB 吗? - V2EX PostgreSql Oracle 迁移 postgreSQL ,有什么经验分享 - V2EX 有什么权限管理最佳实践吗 - V2EX 请教 PostgreSQL 和 PostGIS 使用问题 - V2EX pg 的毫秒级时间戳转换 - V2EX 关于 PostgreSQL 中的 max_connections 和 pg_stat_activity 的疑惑 - V2EX 目前国内最高性价比的 postgresql 服务是? - V2EX 新手,请问 postgres 怎么配置 vacuum - V2EX 学习 PostgreSQL,有什么资料推荐? - V2EX Apache AGE 和 Neo4j 哪个更好? - V2EX 有没有像 Citus 那样方便分片的图数据库? - V2EX 怎样使用 C 语言访问 PostgreSQL 集群? 小系统 数据库 mysql 和 postgresql 比对 和 wordpress 数据库技术选型 PostgreSQL 没有 on update current_timestamp 感觉好麻烦 - V2EX 主从同步使用的端口号可以和数据库端口号不同吗? - V2EX 为什么有时 PostgreSQL 有时会表现得比较卡? - V2EX 第一次使用 PostgreSQL,连建表都不会,请教点经验 Java 有没有针对 PostgreSQL 好用一点的 ORM - V2EX 请教,有无简单易用的 PostgreSQL 表结构、数据的同步工具? - V2EX PostgreSQL 启动失败 - V2EX PostgreSQL 数据库 正常情况下指定了 locale=C 与 encoding=UTF8 (最简单) - V2EX 错误迁移 pgsql 导致日志报 warning,请问如何解决? - V2EX 从 Mongodb 到 PostgreSQL 的大迁移 有无 pg 的入门精进博客或者视频推荐 有什么不通过写脚本的方式,将 postgreSQL 的建表语句转换为 doris 的建表语句吗? - V2EX 新的 PostgreSQL 语言插件 PL/PRQL - V2EX 请教一个经典的 postgres 的 sql 怎么写 - V2EX 洗数据:如何将 Contact 和 ContactTag 多对多关联起来 Postgres 在中国还能起来么? - V2EX PostgreSQL 17 增加增量备份的功能 - V2EX
请教一个关于 PostgreSQL 连接被 Server 关闭的问题 - V2EX
Koril · 2025-03-11 · via PostgreSQL

背景

我在自己的阿里云服务器上( 2C2G ,3M ,Debian 12 )装了一个 PostgreSQL ( 15 ),安装后,仅仅做了以下配置改动:

pg_hba.conf:

添加:host all all 0.0.0.0/0 md5

postgresql.conf

开放端口:listen_addresses = '*'

给 postgres 设置了密码,sudo -i -u postgres -> psql -> \password


异常

无论是 Navicat 还是 Python 的 Psycopg2 在超过一定时间(大概 3-5 分钟),就会连接失效了。

Navicat 报错如下:

Server closed the connection unexpectedly
This probably means the server terminated abnormally before or while processing the request.

Psycopg2 报错如下:

psycopg2.OperationalError: server closed the connection unexpectedly
	This probably means the server terminated abnormally
	before or while processing the request.
server closed the connection unexpectedly
	This probably means the server terminated abnormally
	before or while processing the request. 

已知

服务器没有太大的负载,cpu ,内存,磁盘,网络,占用率都很低,除了 Prometheus/Grafana/nginx 之外,这个机器上只有刚刚安装的 PostgreSQL 。

重新获取连接,能够正常访问,但是我使用的 Psycopg2 的 ThreadedConnectionPool 连接池,连接池没法保持连接么?代码如下:


def get_pg_pool(pg_config):
    pool = psycopg2.pool.ThreadedConnectionPool(
        minconn=1,
        maxconn=200,
        host=pg_config['PG_HOST'],
        port=pg_config['PG_PORT'],
        dbname=pg_config['PG_DB'],
        user=pg_config['PG_USER'],
        password=pg_config['PG_PASSWORD'],
        connect_timeout=5,
    )
    return pool
    
@flask_app.route('/task/log', methods=['GET'])
    def task_log():
        task_log_list = []
        # 在进程启动时,给 flask_app 初始化了一个 pg_pool 对象
        conn = flask_app.pg_pool.getconn()
        try:
            with conn.cursor() as cur:
                cur.execute('SELECT * FROM t_log')
                task_log_list = cur.fetchall()
        except psycopg2.Error as e:
            return JsonResult.failed('获取日志列表失败')
        finally:
            flask_app.pg_pool.putconn(conn)

        return JsonResult.successful(task_log_list)


问题

这个问题是和 Linux 服务器配置有关呢?还是跟 PostgreSQL 配置有关?请问如何排查和解决呢?