You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker中SpringBoot连接PostgreSQL14超时求助,PostgreSQL15正常

PostgreSQL14连接超时问题排查与解决

核心问题分析

从PostgreSQL14的日志可以看到最后一行:

2023-02-28 08:47:15.810 IST [317] LOG:  database system is shut down

说明PostgreSQL14服务并未处于运行状态,这是导致Docker内SpringBoot应用、pgAdmin连接超时的根本原因。PostgreSQL15能正常连接,是因为其服务正常运行且配置无问题。

排查与修复步骤

  1. 检查并启动PostgreSQL14服务

    • Windows:打开服务(services.msc),找到postgresql-x64-14服务,确认状态为正在运行,若未启动则手动启动。
    • Linux:执行systemctl status postgresql-14查看状态,未运行则执行systemctl start postgresql-14,并设置开机自启systemctl enable postgresql-14。
    • macOS:执行brew services list | grep postgresql,若未运行则执行brew services start postgresql@14。
    • 若启动失败,查看系统日志(Windows事件查看器、Linux/var/log/postgresql/目录、macOS控制台)定位启动失败原因。
  2. 验证监听配置(postgresql.conf)

    • 找到PostgreSQL14的配置文件目录(Windows默认C:\Program Files\PostgreSQL\14\data,Linux默认/var/lib/postgresql/14/main,macOS默认/usr/local/var/postgresql@14)。
    • 编辑postgresql.conf:
      • 确保listen_addresses = '*'(允许所有IP访问,或指定特定IP如localhost,host.docker.internal)。
      • 确认port = 5433(与连接字符串中的端口一致,避免和PostgreSQL15默认的5432端口冲突)。
    • 修改后重启PostgreSQL14服务。
  3. 检查端口占用与防火墙

    • 验证5433端口是否被占用:
      • Windows:netstat -ano | findstr 5433
      • Linux/macOS:lsof -i :5433
    • 若端口被其他程序占用,停止占用程序或修改PostgreSQL14的端口。
    • 检查防火墙规则,确保5433端口允许本地及Docker容器访问(Docker默认网段通常为172.17.0.0/16)。
  4. 配置连接权限(pg_hba.conf)

    • 同目录下编辑pg_hba.conf,添加以下规则允许本地、Docker容器及host.docker.internal访问:
      host    test     postgres    127.0.0.1/32    scram-sha-256
      host    test     postgres    172.17.0.0/16   scram-sha-256
      host    test     postgres    host.docker.internal/32    scram-sha-256
      
    • 重启PostgreSQL14服务使配置生效。
  5. 本地连接测试

    • 使用psql命令直接测试本地连接:
      psql -h localhost -p 5433 -U postgres -d test
      
    • 若本地连接成功,再测试Docker内的SpringBoot应用连接;若本地连接失败,优先解决本地连接问题。

内容的提问来源于stack exchange,提问作者Teletubbies

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 10:52:41