0

I have been working on a project which requires us to connect to our PostgreSQL database. I was able to connect locally with my code to the PostgreSQL database using a local IP(i.e. 192.168.1.145) now that I replaced the local IP with the public IP it throws me errors:

Here is the code I am using to connect to the database

private static final String DRIVER = "org.postgresql.Driver";
//private static final String URL = "jdbc:postgresql://192.168.1.145:5432/ResumeDatabase";
private static final String URL = "jdbc:postgresql://71.555.555.555:5432/ResumeDatabase";
private static final String USERNAME = "defaultuser";
private static final String PASSWORD = "password";

private static Connection getConnection() throws Exception {
    Class.forName(DRIVER);
    Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
    return conn;
  }

Here are the errors that I get:

  Adding a resume
    org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port    are correct and that the postmaster is accepting TCP/IP connections.
    ID ----------- 0
    Added Resume
    at
    org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:136)
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125)
    at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
    at org.postgresql.jdbc3.Jdbc3Connection.<init>(Jdbc3Connection.java:24)
    at org.postgresql.Driver.makeConnection(Driver.java:393)
    at org.postgresql.Driver.connect(Driver.java:267)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at com.ssati.resumeformatter.data.dao.ResumeDAO.getConnection(ResumeDAO.java:247)
    at com.ssati.resumeformatter.data.dao.ResumeDAO.addResume(ResumeDAO.java:105)
    at com.ssati.resumeformatter.data.dao.ResumeDAO.main(ResumeDAO.java:318)
Caused by: java.net.ConnectException: Operation timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at java.net.Socket.<init>(Socket.java:375)
    at java.net.Socket.<init>(Socket.java:189)
        at org.postgresql.core.PGStream.<init>(PGStream.java:62)
    at     org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:76)
    ... 11 more

Thanks!

Chris Travers
  • 25,424
  • 6
  • 65
  • 182
roycemathew
  • 29
  • 1
  • 5
  • 3
    Have you ensured that your database is configured to allow remote connections from the target machine with those credentials? The database configuration seems quite pertinent here... – maerics Jul 30 '12 at 18:37
  • What are the errors you're getting? – toniedzwiedz Jul 30 '12 at 18:37
  • 1
    What are contents of `pg_hba.conf`? – barti_ddu Jul 30 '12 at 18:40
  • @Tom : please see above the errors I receive – roycemathew Jul 30 '12 at 18:44
  • @barti_ddu: I have set up SSL to allow all connections in pg_hba.conf – roycemathew Jul 30 '12 at 18:45
  • @maerics: Im not sure I have, how can i tell if I did? – roycemathew Jul 30 '12 at 18:45
  • 1
    @user1563793: For SSL you must add `?ssl=true` to the URL. Although my first guess is that `Operation timed out` indicates some missconfigured firewall. – A.H. Jul 30 '12 at 18:50
  • A.H. +1, see http://jdbc.postgresql.org/documentation/91/connect.html – barti_ddu Jul 30 '12 at 18:57
  • @roycemathew: well, you can tell that you've configured it correctly when you're able to connect with your client =P This page about [the "pg_hba.conf" file](http://www.postgresql.org/docs/9.1/static/auth-pg-hba-conf.html) might be helpful. – maerics Jul 30 '12 at 19:21
  • 1
    `71.555.555.555`: is that a valid IP address? How can 555 represent an unsigned byte? – JB Nizet Jul 30 '12 at 21:14
  • Remember, when you're having issues like this, make sure you can connect with `psql` to eliminate possible issues with your configuration. – Craig Ringer Jul 31 '12 at 01:34
  • @JBNizet I didn't want to give out my public IP so I put a fake one instead – roycemathew Jul 31 '12 at 12:32
  • @CraigRinger I can connect PostgreSQL without Java remotely. So I do believe the configuration is correct, I think its something to do with the JDBC and how JDBC uses a local host – roycemathew Jul 31 '12 at 12:34
  • This is clearly a network issue (connection refused). Check your IP, host name, etc and if not try with psql from the system you are writing Java on. There could be something between the computers (like a firewall) causing the problem too. – Chris Travers Apr 05 '13 at 06:35

0 Answers0