15

I will connect with a simple java programme to my heroku Postgres database. But if i try to connect i get an error. Can anyone explain me the problem or can solve this problem?

Thx :D

Code:

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;
    }...

Output:

-------- 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)
    ...
franklin
  • 1,800
  • 7
  • 32
  • 59
user2372976
  • 715
  • 1
  • 8
  • 19
  • 4
    See here: http://stackoverflow.com/questions/9386803/problems-connecting-pentaho-kettle-spoon-to-heroku-postgresql-using-ssl and here: http://stackoverflow.com/questions/11832361/how-to-setup-spring-heroku-postgres-ssl-datasource – Lukas Eklund Feb 27 '14 at 16:27

3 Answers3

26

Thanks Lukas Eklund!

My url should look like this:

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";
Maroun
  • 94,125
  • 30
  • 188
  • 241
user2372976
  • 715
  • 1
  • 8
  • 19
  • 6
    Working around by not validating the cert isn't great from security standpoint... – Chris Grenz Apr 12 '17 at 16:39
  • see https://jdbc.postgresql.org/documentation/head/ssl-client.html documentation how to set it up correctly including certificates to validate – Markus Oct 05 '17 at 12:49
7

Add sslmode to the database url:

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

SkyWalker
  • 28,384
  • 14
  • 74
  • 132
Hansel
  • 71
  • 1
  • 1
1

I had the same problem but I had to use 2 settings to get it work:

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

ssl=true

and

sslmode=require

Sami Badawi
  • 977
  • 1
  • 10
  • 22