【问题标题】:Failed connection when trying for Heroku PostgreSQL with Spring尝试使用 Spring 连接 Heroku PostgreSQL 时连接失败
【发布时间】:2021-10-21 07:53:19
【问题描述】:

我正在尝试设置 Spring Boot 应用程序。目前我在本地环境中工作,我有一个 Heroku PostgreSQL 数据库。当我启动我的应用程序时,它以错误开头:

HikariPool-1 - Exception during pool initialization.
org.postgresql.util.PSQLException: El intento de conexión falló.
...
Caused by: java.net.UnknownHostException: @host
...
HHH000342: Could not obtain connection to query metadata

为了安全起见,我添加了正确的主机名并将其隐藏。我知道这个主机名是正确的,因为我已经设法使用 Express 应用程序进行连接。

我的 POM.xml 有以下依赖项:}

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

而我的 application.properties 文件是:

spring.datasource.url=jdbc:postgresql://@host:5432/dal2fiqv9or5eg?user=----&password=----&sslmode=require
spring.datasource.username=----
spring.datasource.password=----
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa-hibernate.ddl-auto=create-drop
spring.jpa.show=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true

我再次隐藏了主机、用户名和密码。

在此之后应用程序启动。

【问题讨论】:

    标签: postgresql spring-boot spring-data-jpa heroku-postgres


    【解决方案1】:

    您的 application.properties 中的 spring.datasource.url 属性似乎不正确。

    这个文件对我有用:

        #postgres parameters
    spring.datasource.url=jdbc:postgresql://<host>:<port>/<database>
    spring.datasource.username=<user>
    spring.datasource.password=<password>
    spring.jpa.show-sql=true
    
    ## Hibernate Properties
    # The SQL dialect makes Hibernate generate better SQL for the chosen database
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
    
    # Hibernate ddl auto (create, create-drop, validate, update)
    spring.jpa.hibernate.ddl-auto = update
    
    logging.level.org.springframework=TRACE
    
    logging.level.org.hibernate.type=TRACE
    server.port=${PORT:8080}
    

    端口、主机、数据库、用户和密码的值可以从您添加到 Heroku 应用程序的 HerokuPostgres 插件导出

    不要添加@ 或任何特殊符号。只需将值替换为您在 Heroku 中看到的值。 例如:spring.datasource.url=jdbc:postgresql://myhostname:5432/mydbname

    【讨论】:

    • 感谢 mnagdev!问题是@符号。删除它完成了工作。也许你已经回答了这个问题,但是当我将它部署到 Heroku 中时,是否可以使用 : 语法而不是硬编码值?还是应该使用 heroku CLI 设置环境变量?
    猜你喜欢
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 2010-10-27
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多