尝试部署:org.postgresql.util.PSQLException:连接尝试失败

问题描述 投票:0回答:1

在 render.com 上拥有帐户,我创建了 postgres 数据库并获取了用户名、密码和外部链接(主机,端口为 5432),所以我按照建议指定了 url here (

jdbc:postgresql://<host>:<port>/<dbname>?user=<username>&password=<password>
)。

application.properties

#spring.datasource.url=jdbc:postgresql://localhost:5432/webdev_simplified
spring.datasource.url=jdbc:postgresql://@dpg-cligukdkt82s73c4s830-a.oregon-postgres.render.com:5432/webdev_simplified?user=webdev_simplified_user&password=redacted
#spring.datasource.username=postgres
#spring.datasource.username=webdev_simplified_user
#spring.datasource.password=postgres
#spring.datasource.password=c8Ca0ZTgSkokADeeEeoIM9WFPPjPLXHx

spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.SQL=DEBUG

我使用了他们所说的 external url 从我本地的 ubuntu 连接到它。但是当运行

mvn clean package
时,我得到:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.2.0)

2023-11-28T21:11:52.164+01:00  INFO 16808 --- [           main] com.example.j2.J2ApplicationTests        : Starting J2ApplicationTests using Java 17.0.2 with PID 16808 (started by shepherd in /home/shepherd/Desktop/java/j2)
2023-11-28T21:11:52.166+01:00  INFO 16808 --- [           main] com.example.j2.J2ApplicationTests        : No active profile set, falling back to 1 default profile: "default"
2023-11-28T21:11:53.139+01:00  INFO 16808 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-11-28T21:11:53.220+01:00  INFO 16808 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 69 ms. Found 1 JPA repository interface.
2023-11-28T21:11:53.793+01:00  INFO 16808 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2023-11-28T21:11:54.843+01:00 ERROR 16808 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.

org.postgresql.util.PSQLException: The connection attempt failed.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:315) ~[postgresql-42.2.15.jar:42.2.15]
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51) ~[postgresql-42.2.15.jar:42.2.15]
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:225) ~[postgresql-42.2.15.jar:42.2.15]
...

我确信我的主机是正确的,因为我只是复制/粘贴它(就像 psasword 一样)。那么会出现什么问题呢?

这是源代码: https://github.com/shepherd123456/springapp_with_postgres

PS:错误很长,所以我只选择了我认为可能重要的部分:

...
Caused by: java.net.UnknownHostException: @dpg-cligukdkt82s73c4s830-a.oregon-postgres.render.com
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:567) ~[na:na]
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
    at java.base/java.net.Socket.connect(Socket.java:633) ~[na:na]
    at org.postgresql.core.PGStream.createSocket(PGStream.java:231) ~[postgresql-42.2.15.jar:42.2.15]
    at org.postgresql.core.PGStream.<init>(PGStream.java:95) ~[postgresql-42.2.15.jar:42.2.15]
    at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:98) ~[postgresql-42.2.15.jar:42.2.15]
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:213) ~[postgresql-42.2.15.jar:42.2.15]
    ... 120 common frames omitted

2023-11-29T11:11:33.472+01:00  INFO 4223 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2023-11-29T11:11:33.572+01:00  INFO 4223 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 6.3.1.Final
2023-11-29T11:11:33.639+01:00  INFO 4223 --- [           main] o.h.c.internal.RegionFactoryInitiator    : HHH000026: Second-level cache disabled
2023-11-29T11:11:33.981+01:00  INFO 4223 --- [           main] o.s.o.j.p.SpringPersistenceUnitInfo      : No LoadTimeWeaver setup: ignoring JPA class transformer
2023-11-29T11:11:34.028+01:00  INFO 4223 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2023-11-29T11:11:35.030+01:00 ERROR 4223 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.

org.postgresql.util.PSQLException: The connection attempt failed.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:315) ~[postgresql-42.2.15.jar:42.2.15]
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51) ~[postgresql-42.2.15.jar:42.2.15]
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:225) ~[postgresql-42.2.15.jar:42.2.15]
...


...
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:495) ~[surefire-booter-3.1.2.jar:3.1.2]

2023-11-29T11:11:35.045+01:00 ERROR 4223 --- [           main] j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA EntityManagerFactory: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] due to: Unable to determine Dialect without JDBC metadata (please set 'javax.persistence.jdbc.url', 'hibernate.connection.url', or 'hibernate.dialect')
2023-11-29T11:11:35.047+01:00  WARN 4223 --- [           main] o.s.w.c.s.GenericWebApplicationContext   : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] due to: Unable to determine Dialect without JDBC metadata (please set 'javax.persistence.jdbc.url', 'hibernate.connection.url', or 'hibernate.dialect')
2023-11-29T11:11:35.067+01:00  INFO 4223 --- [           main] .s.b.a.l.ConditionEvaluationReportLogger : 

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-11-29T11:11:35.092+01:00 ERROR 4223 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] due to: Unable to determine Dialect without JDBC metadata (please set 'javax.persistence.jdbc.url', 'hibernate.connection.url', or 'hibernate.dialect')
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1775) ~[spring-beans-6.1.1.jar:6.1.1]
...



...
[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Errors: 
[ERROR]   J2ApplicationTests.contextLoads » IllegalState Failed to load ApplicationContext for [WebMergedContextConfiguration@62ea8931 testClass = com.example.j2.J2ApplicationTests, locations = [], classes = [com.example.j2.J2Application], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [], propertySourceProperties = ["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@74ea2410, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@248e319b, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@548d708a, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@353352b6, org.springframework.boot.test.context.SpringBootTestAnnotation@85af5aae], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
[INFO] 
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
...

所以这就是说

UnknownHostException

Caused by: java.net.UnknownHostException: @dpg-cligukdkt82s73c4s830-a.oregon-postgres.render.com

但话又说回来,主机是从站点复制/粘贴的。您可以在

External Database Url
相同的网址中看到附加的图像(以
@
开头)。我将再次从我的 render.com 帐户复制未修改的内容(有 postgres 而不是 postgresql,并且用户和密码在 url 之前指定,而不是作为上面链接建议的查询参数):

postgres://webdev_simplified_user:c8Ca0ZTgSkokADeeEeoIM9WFPPjPLXHx@dpg-cligukdkt82s73c4s830-a.oregon-postgres.render.com/webdev_simplified
postgresql spring-boot connection-string render.com
1个回答
0
投票

根据 DNS 错误消息并尝试我自己的简单 jdbc 示例,您需要将主机名中的

@
去掉。就像您不能在主机名前面指定用户名和密码一样,您也不能在其前面放置一个裸露的
@
。它被解释为主机名中的文字字符。

可惜,并非所有驱动程序都支持格式化 db URL 的所有方式。

© www.soinside.com 2019 - 2024. All rights reserved.