1

I have two virtual machines where one is hosting postgres DB and another application that is connecting to database. I have tried multiple configuration ways, but every time connecting I'm connecting via JDBC I get:

org.postgresql.util.PSQLException: The connection attempt failed.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:315)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:225)
    at org.postgresql.Driver.makeConnection(Driver.java:465)
    at org.postgresql.Driver.connect(Driver.java:264)
    at java.sql/java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql/java.sql.DriverManager.getConnection(Unknown Source)
Caused by: java.net.SocketTimeoutException: connect timed out
    at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.base/java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.base/java.net.SocksSocketImpl.connect(Unknown Source)
    at java.base/java.net.Socket.connect(Unknown Source)
    at org.postgresql.core.PGStream.createSocket(PGStream.java:231)
    at org.postgresql.core.PGStream.<init>(PGStream.java:95)
    at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:98)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:213)

For now in my pg_hba.conf file I have configured application host with:

host my_db user 192.168.14.147/32 password (tried md5 and even trust, same result)

I have also checked postgresql.conf and I have

listen_addresses = '*'

I have reloaded conf in db with:

SELECT pg_reload_conf();

My postgresql server is working on host 192.168.44.38. Before I've added pg_hba.conf configuration I was getting error about no entry in this file and I also tried to connect to DB via pgAdmin and it works, so my config should be okay.

My jdbc connection method:

 try {
        String postgresConnectionUrl = "jdbc:postgresql://192.168.44.38:5432/my_db";
        Connection connection = DriverManager.getConnection(postgresConnectionUrl, "user", "my_pass");
        return connection;
    } catch (SQLException sqlException) {
        log.error("Error connecting. ", sqlException);
        return null;
    }

I've found nothing in postgres logs, also my user account has no limited connections and isn't even used.

4
  • 4
    "I've found nothing in postgres logs" - because the traffic never made it to Postgres, it was blocked at the network level. The problem is in the network/firewall configuration at the server. Commented Jan 24, 2023 at 20:15
  • "Before I've added pg_hba.conf configuration I was getting error about no entry in this file" You must have broke something, because now you are failing even earlier in the process than it was before. Or were those errors coming when connecting from a different server? Commented Jan 25, 2023 at 1:37
  • Where was pgAdmin installed? On the same server as your app, the same server as the database, or somewhere else? Commented Jan 25, 2023 at 1:38
  • Problem solved. Network configuration only allowed connections from 192.168.44.x adressess. Thanks for your advices. Commented Jan 30, 2023 at 7:34

0

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.