简体   繁体   English

我无法连接到 Heroku Postgres 数据库

[英]I am unable to connect to the Heroku Postgres database

I'm trying to connect to the bank, but it's giving this error here when I try:我正在尝试连接到银行,但是当我尝试时它在这里给出了这个错误:


APPLICATION FAILED TO START应用程序无法启动


Description:描述:

Failed to configure a DataSource: no embedded datasource could be configured.无法配置数据源:无法配置嵌入式数据源。

Reason: Failed to determine a suitable driver class原因:无法确定合适的驱动程序 class

Action:行动:

Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.考虑以下几点:如果您想要一个嵌入式数据库(H2、HSQL 或 Derby),请将其放在类路径中。 If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).如果您有要从特定配置文件加载的数据库设置,您可能需要激活它(当前没有配置文件处于活动状态)。

My application properties:我的应用程序属性:

spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.datasource.url=jdbc:postgres://********:******:port/database
spring.datasource.username=******
spring.datasource.password=*** 

spring.jpa.hibernate.ddl-auto=update
spring.http.cors.enabled=true

My pom:我的pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bird</groupId>
    <artifactId>bird</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>bird</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <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.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Please add below property in your application.properties though spring detects it based on URL请在您的application.properties中添加以下属性,尽管 spring 根据 URL 检测到它

spring.datasource.driver-class-name=org.postgresql.Driver

Also please ensure you have below dependency in your pom.xml另外请确保您在 pom.xml 中具有以下依赖项

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
</dependency>

Set followings设置以下

In your application.properties在你的application.properties

spring.datasource.url=jdbc:postgresql://<HOST>:<PORT>/<DATABASE>
spring.datasource.username=<USER>
spring.datasource.password=<PASSWORD>
spring.datasource.driver-classname=org.postgresql.Driver

pom.xml pom.xml

<dependencies>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.2.20</version>
    </dependency>
</dependencies>

From your heroku postgres administration Settings option you can inspect the values从 heroku postgres 管理设置选项中,您可以检查值

Postgres 管理设置

Bonus: More appropriate option would be capture the application.properties values from environment.奖励:更合适的选项是从环境中捕获application.properties值。 To do so change your application properties like following为此,请更改您的应用程序属性,如下所示

spring.datasource.url=${SPRING_DATASOURCE_URL}
spring.datasource.username=${SPRING_DATASOURCE_USERNAME}
spring.datasource.password=${SPRING_DATASOURCE_PASSWORD}
spring.datasource.driver-classname=org.postgresql.Driver

Then in heroku dashboard your application settings there is a option named Reveal config var .然后在 heroku 仪表板您的应用程序设置中有一个名为Reveal config var的选项。

在此处输入图像描述

There add following environment variables添加以下环境变量

SPRING_DATASOURCE_URL=jdbc:postgresql://<HOST>:<PORT>/<DATABASE>
SPRING_DATASOURCE_USERNAME=<USER>
SPRING_DATASOURCE_PASSWORD=<PASSWORD>

Thus your application stays decoupled from the environment.因此,您的应用程序与环境保持分离。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM