1. 问题背景
在Spring Boot项目中配置PostgreSQL数据源时,一个常见的坑就是提供了错误的数据库密码,或者干脆忘了密码。这会导致项目启动连接数据库时抛出异常。本文将教你如何避免PSQLException
这个恼人的错误。
2. 数据源配置
Spring Boot中配置数据源主要有两种方式:application.properties
或application.yml
。注意:两种方式二选一,不能同时使用。
2.1. 使用application.properties
创建并配置application.properties
文件,至少包含以下必填字段:
spring.datasource.url=jdbc:postgresql://localhost:5432/tutorials
spring.datasource.username=postgres
spring.datasource.password=
spring.jpa.generate-ddl=true
通过spring.jpa.generate-ddl=true
让应用启动时自动创建表。
故意留空密码字段,然后启动应用:
mvn spring-boot:run
果然报错了,因为没提供密码:
org.postgresql.util.PSQLException: The server requested password-based authentication, but no password was provided.
如果使用较新版本的PostgreSQL,错误信息可能略有不同,会显示SCRAM认证相关错误:
org.postgresql.util.PSQLException: The server requested SCRAM-based authentication, but no password was provided.
2.2. 使用application.yml
先注释或删除application.properties
文件,避免冲突。然后创建并配置application.yml
文件:
spring:
datasource:
url: jdbc:postgresql://localhost:5432/tutorials
username: postgres
password: ""
jpa:
generate-ddl: true
同样留空密码字段启动应用,会得到和之前完全相同的错误。新版本PostgreSQL也会显示SCRAM认证错误。
2.3. 正确提供密码
无论使用哪种配置方式,只要在密码字段填入正确密码,应用就能正常启动。
如果密码错误,会收到明确的报错:
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "postgres"
使用正确密码后,连接成功,应用正常启动:
2024-07-19T00:03:33.429+03:00 INFO 18708 --- [ restartedMain] com.baeldung.boot.Application : Started Application in 0.484 seconds
2024-07-19T00:03:33.179+03:00 INFO 18708 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-9 - Starting...
2024-07-19T00:03:33.246+03:00 INFO 18708 --- [ restartedMain] com.zaxxer.pool.HikariPool : HikariPool-9 - Added connection org.postgresql.jdbc.PgConnection@76116e4a
2024-07-19T00:03:33.247+03:00 INFO 18708 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-9 - Start completed.
3. 数据库密码重置
如果忘了密码或想修改密码,可以通过以下方式重置。这里以重置默认用户postgres
的密码为例。
3.1. 重置默认用户密码
步骤分解:
- 找到PostgreSQL的data目录(Windows下通常在
C:\Program Files\PostgreSQL\16\data
) - 备份
pg_hba.conf
文件(防止改错):copy "pg_hba.conf" "pg_hba.conf.backup"
- 修改
pg_hba.conf
文件:将所有本地连接的认证方式从scram-sha-256
改为trust
,这样就能无密码登录:# TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all trust # IPv4 local connections: host all all 127.0.0.1/32 trust # IPv6 local connections: host all all ::1/128 trust # Allow replication connections from localhost, by a user with the # replication privilege. local replication all trust host replication all 127.0.0.1/32 trust host replication all ::1/128 trust
- 重启PostgreSQL服务(Windows):
pg_ctl -D "C:\Program Files\PostgreSQL\16\data" restart
- 使用
psql
无密码登录:psql -U postgres
- 修改密码:
ALTER USER postgres WITH PASSWORD 'new_password';
- 恢复
pg_hba.conf
文件并重启PostgreSQL服务
现在就能用新密码连接数据库了。
3.2. 重置其他用户密码
使用psql
重置任意用户密码:
- 以
postgres
用户登录:psql -U postgres
- 输入密码登录后,执行:
ALTER USER user_name WITH PASSWORD 'new_password';
4. 总结
本文介绍了Spring Boot连接PostgreSQL时常见的密码认证问题及解决方案。关键点包括:
✅ 正确配置数据源密码
✅ 理解不同PostgreSQL版本的认证方式差异
✅ 掌握数据库密码重置的完整流程
完整示例代码可在GitHub仓库获取。