Connected to DB using ssh, JDBC refuses connection

Answered

I added my connection successfully to IntelliJ and I'm able to see my DB schemas. However when I try to run my application, it fails on getConnection()

I'm pretty sure I have it set up correctly as I have the URL copied from the IntelliJ DB connection properties window, are there extra steps required for ssh tunneling to work? Because I have the feeling that JDBC is trying to access the database locally rather than through the ssh tunnel.

URL from properties window: jdbc:postgresql://localhost:5432/<dbname>

Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/<dbname>", username, password);

 org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

Thank you!

1
2 comments

SSH tunnel works only for the IDE features, it's not enabled for your code. If you need to connect via a tunnel, create it externally using command line SSH or adjust your code so that it creates the tunnel when needed.

If you are on Windows, a tool like https://www.bitvise.com/ssh-client may simplify the tunnel creation for code debugging.

1
Avatar
Permanently deleted user
0

Please sign in to leave a comment.