1

I have a Heroku PostgreSQL database to which I can't connect from Java. I have tried different methods but without success.

Below is my Java code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;



public class MainConfig {

    public static void main(String[] args) {

     try{
         dataSource();
         System.out.println("conected");
     }
    catch(Exception ex){
        ex.printStackTrace();
    }
    }

    public static Connection dataSource() throws URISyntaxException, SQLException {

        String ConnectionString ="jdbc:postgresql://l********n:cPd7VRjIhNVWOrZ4uWEeZWKYqV@ec2-184-73-251-115.compute-1.amazonaws.com:5432/dac36a93ohuvsd";
        String username = "****";
        String password = "***";

        return DriverManager.getConnection(ConnectionString, username, password);
    }

}

The error I get in console is:

> org.postgresql.util.PSQLException: The connection attempt failed.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:233)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:144)
    at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
    at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:21)
    at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:31)
    at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
    at org.postgresql.Driver.makeConnection(Driver.java:410)
    at org.postgresql.Driver.connect(Driver.java:280)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at MainConfig.dataSource(MainConfig.java:34)
    at MainConfig.main(MainConfig.java:14)
Caused by: java.net.UnknownHostException: lxlrnhejlzmyun:cPd7VRjIhNVWOrZ4uWEeZWKYqV@ec2-184-73-251-115.compute-1.amazonaws.com
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at org.postgresql.core.PGStream.<init>(PGStream.java:61)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:109)
    ... 12 more

I have added to my Build Path the Java driver postgresql-9.3-1102.jdbc41.jar

Another method I have tried for connecting is described bellow with the error message:

    import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;



public class MainConfig {

    public static void main(String[] args) {

     try{
         dataSource();
         System.out.println("conected");
     }
    catch(Exception ex){
        ex.printStackTrace();
    }
    }

    public static Connection dataSource() throws URISyntaxException, SQLException {
        URI dbUri = new URI(System.getenv("postgres://l**********n:cPd7VRjIhNVWOrZ4uWEeZWKYqV@ec2-184-73-251-115.compute-1.amazonaws.com:5432/dac36a93ohuvsd"));

        String username = dbUri.getUserInfo().split(":")[0];
        String password = dbUri.getUserInfo().split(":")[1];
        String ConnectionString = "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath();



        return DriverManager.getConnection(ConnectionString, username, password);
    }

}

Error

> java.lang.NullPointerException
    at java.net.URI$Parser.parse(Unknown Source)
    at java.net.URI.<init>(Unknown Source)
    at MainConfig.dataSource(MainConfig.java:23)
    at MainConfig.main(MainConfig.java:14)

What I am doing wrong?

2
  • Caused by: java.net.UnknownHostException: lxlrnhejlzmyun:cPd7VRjIhNVWOrZ4uWEeZWKYqV@ec2-184-73-251-115.compute-1.amazonaws.com. Have you verified the host details ? Commented Aug 2, 2014 at 13:41
  • Possible duplicate of Connecting to Heroku Postgres from Spring Boot Commented May 13, 2019 at 15:14

3 Answers 3

7

Inorder to connect to heroku's PostgreSQL (with the required ssl) you must append the following to the url:

?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory

So the correct url as provided in the answer above is:

jdbc:postgresql://host:port/database?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory

Without these perimeters you will get the error described:

error: no pg_hba.conf entry for host 
Sign up to request clarification or add additional context in comments.

Comments

3

The format should be: jdbc:postgresql://host:port/database, whereas in your code its is, note couple of ':' jdbc:postgresql://l********n:cPd7VRjIhNVWOrZ4uWEeZWKYqV@ec2-184-73-251-115.compute-1.amazonaws.com:5432/dac36a93ohuvsd"

3 Comments

Thank u, I modified the connection string I now i get the error: no pg_hba.conf entry for host "92.82.172.24", user "", database "". I mention that I have inserted the line into pg**.conf and restarted the system # TYPE DATABASE USER ADDRESS METHOD # IPv4 local connections: host all all *********/32 md5 host ************** ********** 92.82.172.24/32 md5
Thank u, I give up with Heroku. I dont't understand how to set up SSL to true. I will use my local database and I will import to another station with SQL query.
0

your url should look like - jdbc:postgresql://hostname:port/dbname?sslmode=require

copy hostname(Host) port and dbname(Database) as mentioned in heroku site

Dont forget to append ?sslmode=require in your url otherwise u won't get connected

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.