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>
最终解决方法
问题根源为端口配置不匹配,解决步骤:
- 确认PostgreSQL实际监听端口:
- 执行
ps aux | grep postgres查看进程启动参数,或打开postgresql.conf查找port配置项,确认数据库实际使用的端口(默认5432)。
- 执行
- 修正Spring Boot数据库连接配置:
- 在
application.properties中确保URL包含正确端口:spring.datasource.url=jdbc:postgresql://localhost:实际端口号/你的数据库名 - 若使用
application.yml:spring: datasource: url: jdbc:postgresql://localhost:实际端口号/你的数据库名
- 在
- 重启Spring Boot应用,验证数据库连接是否正常。
内容的提问来源于stack exchange,提问作者workwise
相关产品推荐
相关产品推荐

