更新:在我这边这是一个愚蠢的端口配置问题——但在下面回答我是如何解决它的,以防万一它对某人有帮助。
在我的 spring boot 应用程序中,在 Ubuntu 22 上运行 PostgreSQL 14.7,我在尝试连接到 postgresql 服务器时遇到密码身份验证错误:
org.postgresql.util.PSQLException: FATAL: password authentication failed for user
我试过
password
, md5
, trust
和 scram-sha-256
作为pg_hba.conf
中的设置
# 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
我可以通过命令行连接用户名/密码组合。以防万一,我正在使用
mvn spring-boot:run
命令来运行 spring boot 应用程序。
我在某处读到将 postgresql 驱动程序升级到特定版本有帮助,所以我在
pom.xml
:中添加了这个版本
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
<version>42.5.0</version>
</dependency>
如何识别spring boot应用程序连接的问题并修复?请帮忙!
所以首先我尝试检查错误日志,但没有记录连接尝试。我开始知道默认情况下不记录连接,所以打开它们:
log_connections = on
在文件中
/etc/postgresql/14/main/postgresql.conf
(在我的例子中,在 ubuntu linux 上)
然后,错误日志(在我的例子中是 ubuntu 上的文件
/var/log/postgresql/postgresql-14-main.log
)显示,在显示正常套接字连接时,jdbc 连接没有到达服务器。
这让我交叉检查了 spring boot 应用程序和 postgresql conf 文件中的端口,我发现
application.xml
使用的连接端口与默认端口不同 5434
(但看起来与默认端口非常相似港口,这就是为什么我在早些时候的粗略检查中没有注意到它)。