org.postgresql.util.PSQLException:致命:从 Spring Boot 连接到 postgres 时用户“postgres”的密码验证失败
Posted
技术标签:
【中文标题】org.postgresql.util.PSQLException:致命:从 Spring Boot 连接到 postgres 时用户“postgres”的密码验证失败【英文标题】:org.postgresql.util.PSQLException: FATAL: password authentication failed for user "postgres" while connecting to postgress from Spring Boot 【发布时间】:2020-09-18 18:52:06 【问题描述】:This is the command prompt where I have created container and database 应用程序.yml
app:
datasource:
jdbc-url: jdbc:postgresql://localhost:5432/postgresDB
username: postgres
password: password
pool-size: 30
我是 docker 新手。我在 docker 中创建了一个容器,并在该容器中创建了 Postgres DB,但是在连接到数据库时出现以下错误:
堆栈跟踪:
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "postgres"
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:525) ~[postgresql-42.2.12.jar:42.2.12]
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:146) ~[postgresql-42.2.12.jar:42.2.12]
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:197) ~[postgresql-42.2.12.jar:42.2.12]
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.2.12.jar:42.2.12]
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:211) ~[postgresql-42.2.12.jar:42.2.12]
at org.postgresql.Driver.makeConnection(Driver.java:459) ~[postgresql-42.2.12.jar:42.2.12]
at org.postgresql.Driver.connect(Driver.java:261) ~[postgresql-42.2.12.jar:42.2.12]
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477) [HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560) [HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) [HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) [HikariCP-3.4.5.jar:na]
at org.flywaydb.core.internal.jdbc.JdbcUtils.openConnection(JdbcUtils.java:56) [flyway-core-6.4.1.jar:na]
at org.flywaydb.core.internal.jdbc.JdbcConnectionFactory.<init>(JdbcConnectionFactory.java:80) [flyway-core-6.4.1.jar:na]
at org.flywaydb.core.Flyway.execute(Flyway.java:456) [flyway-core-6.4.1.jar:na]
at org.flywaydb.core.Flyway.migrate(Flyway.java:159) [flyway-core-6.4.1.jar:na]
at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:65) [spring-boot-autoconfigure-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855) [spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792) [spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) [spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) [spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) [spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) [spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) [spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) [spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) [spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:895) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at com.freecodecamp.freecodecamp.FreecodecampApplication.main(FreecodecampApplication.java:10) ~[classes/:na]
2020-05-31 15:40:31.124 WARN 18004 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.internal.exception.FlywaySqlException:
Unable to obtain connection from database: FATAL: password authentication failed for user "postgres"
【问题讨论】:
您是如何启动应用程序和数据库容器的?请编辑问题以将这些详细信息作为文本包含在问题中,而不是在链接后面,也不是作为终端窗口的屏幕截图。 Docker 中的localhost
通常表示“这个容器”(而不是“这个主机”),因此这通常不是应用程序指定数据库的正确位置。
【参考方案1】:
正如你在tracktrace中看到的,
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "postgres"
您的应用程序无法登录到您的数据库,因为您的凭据不正确。您确定用户名:postgres 和密码:密码吗?
您可以尝试使用 DBMS 使用这些凭据连接到您的数据库吗?
【讨论】:
i.stack.imgur.com/2w8yr.png 在这里,我提供了与 postgress 连接相同的凭据 您正在向 docker 提供“密码”,但实际上在您的数据库上,用户“postgres”没有使用该密码。尝试直接通过 psql 将“password”设置为用户“postgres”的密码:ALTER USER postgres WITH PASSWORD 'password';
以上是关于org.postgresql.util.PSQLException:致命:从 Spring Boot 连接到 postgres 时用户“postgres”的密码验证失败的主要内容,如果未能解决你的问题,请参考以下文章