Spring JPA无法连接到Postgresql

时间:2016-10-14 10:56:49

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

我正在尝试用Postgres编写Spring Data JPA应用程序。

我收到错误:FATAL: password authentication failed for user

我尝试使用常规JDBC编写连接时使用相同的东西,并且所有工作正常,相同的用户名/密码/数据库/主机名。

我用于Spring Data JPA的属性文件包含以下内容:

spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
spring.datasource.username=test_java 
spring.datasource.password=easy_password
server.port=8080

与我用于java jdbc测试的属性文件相比:

db.url=jdbc:postgresql://localhost:5432/testdb
db.user=test_java
db.passwd=easy_password

Java代码如下所示:

public void testSelectWithPropertyFile() {
    Logger lgr = Logger.getLogger(getClass().getName());
    Connection con = null;
    PreparedStatement pst = null;
    ResultSet rs = null;

    Properties props = new Properties();
    FileInputStream in = null;

    try {
        in = new FileInputStream("target/classes/properties/database.properties");
        props.load(in);

    } catch (IOException ex) {
        lgr.log(Level.SEVERE, ex.getMessage(), ex);
        return;
    } finally {

        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        }
    }

    String url = props.getProperty("db.url");
    String user = props.getProperty("db.user");
    String passwd = props.getProperty("db.passwd");

    try {

        con = DriverManager.getConnection(url, user, passwd);
        pst = con.prepareStatement("SELECT * FROM Authors");
        rs = pst.executeQuery();

        while (rs.next()) {
            System.out.print(rs.getInt(1));
            System.out.print(": ");
            System.out.println(rs.getString(2));
        }

    } catch (Exception ex) {
        lgr.log(Level.SEVERE, ex.getMessage(), ex);

    } finally {

        try {
            if (rs != null) {
                rs.close();
            }
            if (pst != null) {
                pst.close();
            }
            if (con != null) {
                con.close();
            }

        } catch (SQLException ex) {
            lgr.log(Level.WARNING, ex.getMessage(), ex);
        }
    }
}

这是我在Spring Boot JPA中得到的错误日志:

2016-10-14 11:09:21.593  INFO 6948 --- [           main] com.example.JpaPostgresApplication       : Starting JpaPostgresApplication on DESKTOP-53J32BH with PID 6948 (C:\Users\charb\workspace\JPA_POSTGRES\target\classes started by charb in C:\Users\charb\workspace\JPA_POSTGRES)
    2016-10-14 11:09:21.595  INFO 6948 --- [           main] com.example.JpaPostgresApplication       : No active profile set, falling back to default profiles: default
    2016-10-14 11:09:21.679  INFO 6948 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@72f926e6: startup date [Fri Oct 14 11:09:21 BST 2016]; root of context hierarchy
    2016-10-14 11:09:23.402  INFO 6948 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$72d9bd3b] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2016-10-14 11:09:23.922  INFO 6948 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
    2016-10-14 11:09:23.933  INFO 6948 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
    2016-10-14 11:09:23.934  INFO 6948 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.5
    2016-10-14 11:09:24.062  INFO 6948 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2016-10-14 11:09:24.062  INFO 6948 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2387 ms
    2016-10-14 11:09:24.230  INFO 6948 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
    2016-10-14 11:09:24.234  INFO 6948 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
    2016-10-14 11:09:24.235  INFO 6948 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
    2016-10-14 11:09:24.235  INFO 6948 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
    2016-10-14 11:09:24.235  INFO 6948 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
    2016-10-14 11:09:24.478  INFO 6948 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
    2016-10-14 11:09:24.492  INFO 6948 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
        name: default
        ...]
    2016-10-14 11:09:24.554  INFO 6948 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.0.11.Final}
    2016-10-14 11:09:24.555  INFO 6948 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
    2016-10-14 11:09:24.557  INFO 6948 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
    2016-10-14 11:09:24.602  INFO 6948 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
    2016-10-14 11:09:24.828 ERROR 6948 --- [           main] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.

    org.postgresql.util.PSQLException: FATAL: password authentication failed for user "test_java "
        at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:446) ~[postgresql-9.4.1211.jre7.jar:9.4.1211.jre7]
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:220) ~[postgresql-9.4.1211.jre7.jar:9.4.1211.jre7]

我无法理解Spring数据JPA如何在普通的Java JDBC工作正常时无法进行身份验证。

2 个答案:

答案 0 :(得分:0)

您的问题是身份验证,

Se留言

  

用户" test_java"

的密码验证失败

确定此信息连接正确吗?因为那是你的问题 如果是正确的,那么进行测试,连接你的基地中的同一个其他客户端,与本地。

  • 如果可以的话。问题是它没有能力连接远程。
  • 对于能力连接远程访问pg_hba.config然后能力远程连接(或配置,您的数据库的其他文件)

请参阅:我文件配置的那部分:

# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
host    all             all             192.168.2.0/24            md5
许可连接本地和网络192.168.2.0,有很多方式配置

答案 1 :(得分:0)

spring.database.driverClassName=org.postgresql.Driver替换为 spring.datasource.driverClassName=org.postgresql.Driver