无法从Spring Boot应用程序连接到PostgreSQL数据库 - 致命:数据库不存在

时间:2018-04-07 10:31:53

标签: postgresql spring-boot

我刚用https://start.spring.io/配置了新项目 我的pom.xml:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>tower</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>tower</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>9</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</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>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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>

和我的application.properties:

spring.datasource.url= jdbc:postgresql://localhost:5432/tower

spring.datasource.username=postgres
spring.datasource.password=XXX


spring.jpa.hibernate.ddl-auto=none

spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false

我收到了错误org.postgresql.util.PSQLException: FATAL: database "tower" does not exist。它说我的数据库没有启动,但我可以轻松地使用pgAdmin3 / 4进入它并运行查询。我也使用localhost:5432与数据库名塔,用户postgres和我的密码,这是肯定的。为什么然后我得到并且错误我的数据库没有启动。 我也尝试过:

Class.forName("org.postgresql.Driver");
Connection connection = null;
connection = DriverManager.getConnection(
                "jdbc:postgresql://localhost:5432/tower","postgres", "XXX");

并得到完全相同的错误 - 数据库&#34;塔&#34;不存在。 数据库启动时为什么会出现这样的错误?

1 个答案:

答案 0 :(得分:1)

打开日志记录,至少你会看到你是否尝试在Postgesql中连接。也可以在不使用PGAdmin的情况下尝试,例如使用Squirrel SQL客户端,与PGAdmin不同,它将要求您准确设置连接,即URL = jdbc:postgresql:// localhost:5432 / tower 如果Squirrel连接,那么它比你的代码更像PGAdmin