2

Hi I am trying to connect to a local Postgres Db running in Virtualbox Centos, which connects through SSL tunnel. Example of how I connect via DBeaver is below.

  1. DBeaver I am using a SSL tunnel tab and
  2. I am using a pem file, and user and password

Postgres local connectionenter image description here

  1. The Virtualbox is set to forward port as follows

Postgres Virtualbox port forwardenter image description here

My Java code without ssl tunnel is this

DriverManager.getConnection("jdbc:postgresql://localhost:5432/dbName?user=postgresuser&password=givepassword");

This is failing saying pg_hba.conf not found etc or if I give ssl=true in connection string , says its not supported.. How do I connect to Local DB using ssl tunnel? [ My assumption in that pg_hba.conf file would be available on the Virtualbox Host Vm only.]

Nx Fi
  • 21
  • 4
  • can you post the java code? – Woot4Moo Jun 27 '17 at 15:21
  • I dont have any other except above: this one connects :: `DriverManager.getConnection("jdbc:postgresql://localhost:5432/dbName?user=postgresuser&password=givepassword");` – Nx Fi Jun 27 '17 at 15:34
  • does this work - https://stackoverflow.com/questions/16835761/postgresql-via-ssh-tunnel – Woot4Moo Jun 27 '17 at 15:36
  • It already works, I am able to connect. The issues is not if i can connect or not. I want code example in Java how to do same. – Nx Fi Jun 28 '17 at 15:12
  • @NxFi did you manage to get a working example to replicate what DBeaver was doing to establish the ssh tunnel? can you share your solution – bachr Dec 13 '21 at 19:55

1 Answers1

0

You may try using jsch which is an implementation of SSH in Java.

Here is a complete example of how to do it https://cryptofreek.org/2012/06/06/howto-jdbc-over-an-ssh-tunnel/

Note that in this case all your traffic will go to the proxy hence you need to close the session after reading/writting to the database.

bachr
  • 5,780
  • 12
  • 57
  • 92