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

Ubuntu环境下Spring Boot连接PostgreSQL认证失败排查求助

Spring Boot连接PostgreSQL密码认证错误排查与解决(端口配置问题导致)

在Ubuntu 22系统上运行的Spring Boot应用连接PostgreSQL 14.7时,出现密码认证错误:

org.postgresql.util.PSQLException: FATAL: password authentication failed for user

已做的排查尝试:

  • 调整pg_hba.conf的认证规则,先后尝试password、md5、trust、scram-sha-256等认证方式,配置片段如下:
# IPv4 local connections:
#host    all             all             127.0.0.1/32            scram-sha-256
host    all             all             127.0.0.1/32            trust
#host    all             all             127.0.0.1/32            password

# IPv6 local connections:
#host    all             all             ::1/128                 scram-sha-256
host    all             all             ::1/128                 trust
#host    all             all             ::1/128                  password
  • 命令行使用相同用户名和密码可正常连接数据库
  • 升级PostgreSQL JDBC驱动至42.5.0版本,pom.xml配置如下:
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
    <version>42.5.0</version>
</dependency>

最终解决方法

问题根源为端口配置不匹配,解决步骤:

  1. 确认PostgreSQL实际监听端口:
    • 执行ps aux | grep postgres查看进程启动参数,或打开postgresql.conf查找port配置项,确认数据库实际使用的端口(默认5432)。
  2. 修正Spring Boot数据库连接配置:
    • 在application.properties中确保URL包含正确端口:
      spring.datasource.url=jdbc:postgresql://localhost:实际端口号/你的数据库名
      
    • 若使用application.yml:
      spring:
        datasource:
          url: jdbc:postgresql://localhost:实际端口号/你的数据库名
      
  3. 重启Spring Boot应用,验证数据库连接是否正常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 20:32:26