Skip to content

How to SSH tunnel and connect to a database using IntelliJ and JDBC?

I’m having issues connecting to a database hosted on a server using IntelliJ and JDBC. With the command line, the command:

ssh username@server -L 11100:ct1:28017 -N

creates a tunnel successfully and the command:

psql db_name -h localhost -p 11100 db_username

connects to the database fine and allows me to use it. How would this be translated correctly into the IntelliJ Data Sources and the SSH tunnel tools?

At the moment, for the SSH tunnel settings I have:

Proxy Host: server

Port: 22

Proxy user: username

For the general tab I have:

host: localhost

port: 28017

database: db_name

user: db_username

The connection returns successfully after testing the connection. Inside my program I have:

Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost:28017/db_name", user, password);

The URL parameter is the one generated by IntelliJ. This however gives me an error “Connection to localhost:28017 refused.”

Any ideas on what I am doing wrong?



Do you connect to SSH server from a java client like SSHJ ( ?

The URL on your DriverManager seems wrong: try port 11100 instead.

5 People found this is helpful