无法连接到heroku PostgreSQL数据库

时间:2014-02-27 10:27:25

标签: java postgresql heroku jdbc

我将使用一个简单的java程序连接到我的heroku Postgres数据库。但如果我尝试连接我得到一个错误。任何人都可以解释我的问题或解决这个问题吗?

Thx:D

代码:

public static void main(String[] args) throws InstantiationException,
        IllegalAccessException, ClassNotFoundException {
    System.out.println("-------- PostgreSQL "
            + "JDBC Connection Testing ------------");
    try {
        Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException e) {
        System.out.println("Where is your PostgreSQL JDBC Driver? "
                + "Include in your library path!");
        e.printStackTrace();
        return;
    }
    System.out.println("PostgreSQL JDBC Driver Registered!");
    Connection connection = null;
    try {
        // ========>     from heroku website
        String url = "jdbc:postgresql://ec2-107-20-214-225.compute-1.amazonaws.com:5432/databasename";
        Properties props = new Properties();
        props.setProperty("user", "someusername");
        props.setProperty("password", "somepassword");
        props.setProperty("ssl", "true");
        connection = DriverManager.getConnection(url, props);
    } catch (SQLException e) {

        System.out.println("Connection Failed! Check output console");
        e.printStackTrace();
        return;
    }...

输出:

-------- PostgreSQL JDBC Connection Testing ------------
PostgreSQL JDBC Driver Registered!
Connection Failed! Check output console
org.postgresql.util.PSQLException: The connection attempt failed.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:225)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:138)
    at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
...
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    ...

3 个答案:

答案 0 :(得分:26)

感谢Lukas Eklund!

我的网址应如下所示:

String url = "jdbc:postgresql://ec2-107-20-214-225.compute-1.amazonaws.com:5432/databasename
?user=someusername&password=somepassword
&ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory";

答案 1 :(得分:7)

sslmode 添加到数据库网址:

  

String url =   &#34; JDBC:PostgreSQL的://ec2-107-20-214-225.compute-.amazonaws.com:5432 /数据库名称sslmode =需要

答案 2 :(得分:1)

我遇到了同样的问题,但我必须使用2个设置才能使其正常工作:

String url = "jdbc:postgresql://ec2-107-20-214-225.compute-.amazonaws.com:5432/databasename?ssl=true&sslmode=require

ssl=true

sslmode=require