0
votes

I'm trying to connect to a postgresql server with java, over an SSH tunnel. I do not have administrator access to the postgresql server so I can't modify any setting.

Here's my code:

public SSHConnection() {
    JSch jsch = new JSch();
    //First ssh connection to the first domain (SERVER1_DOMAIN)
    serverSession = jsch.getSession(USERNAME, SERVER1_DOMAIN, 22);
    serverSession.setPassword(PASSWORD);
    localUserInfo lui=new localUserInfo();
    serverSession.setUserInfo(lui);
    serverSession.setConfig(STRICT_HOST_KEY_CHECKING, NO);
    System.out.println("Establishing Connection with "+SERVER1_DOMAIN);
    //Port forwarding to the second domain (SERVER2_DOMAIN)
    serverSession.setPortForwardingL(2233, SERVER2_DOMAIN, 22);
    serverSession.connect();
    System.out.println("Connection established with "+SERVER1_DOMAIN);
    serverSession.openChannel("direct-tcpip");

    //Creating second ssh session
    dbstudSession = jsch.getSession(USERNAME, "localhost", 2233);
    dbstudSession.setPassword(PASSWORD);
    dbstudSession.setUserInfo(lui);
    dbstudSession.setConfig("StrictHostKeyChecking", "no");

    dbstudSession.connect(); // now we're connected to the secondary system
    //Creating port forwarding from my system (6786) to postgresql port of SERVER2_DOMAIN over the ssh connection created above
    dbstudSession.setPortForwardingL(6786,"localhost",5432);
    System.out.println("Connection established with "+SERVER2_DOMAIN);
    Class.forName("org.postgresql.Driver");
    Connection connection = null;
    //Trying to connect to the psql database
    connection = DriverManager.getConnection("jdbc:postgresql://localhost:6786/databasename",USERNAME,PASSWORD);
    connection.close();

}

When I run it, it correctly creates the two ssh connections, but I get this error from the postgre:

FATAL: no pg_hba.conf entry for host "::1", user "taschinfed", database "taschinfed", SSL off at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:473) at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:205) at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) at org.postgresql.jdbc.PgConnection.(PgConnection.java:195) at org.postgresql.Driver.makeConnection(Driver.java:452) at org.postgresql.Driver.connect(Driver.java:254) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at SSHConnection.connect(SSHConnection.java:65) at SSHConnection.(SSHConnection.java:54) at Main.main(Main.java:10)

I don't know what to do

Edit: I can easily access the database through windows power shell, by creating an ssh connection to the first domain, and another ssh connection from the first domain to the second domain, using then the psql command. This means that the ssh connections have no problem, and in fact I can reach the server from my java code, but I don't understand what does that error message means.

1
You forgot to add details like how you set up the ssh tunnel, how listen_addresses is configured in PostgreSQL, the output of netstat -an | grep 5432 on the database server.Laurenz Albe
I have no access to the server settings, therefore I didn't set anything on the server and I cannot access the config file. I edited the question by adding the result of the netstat -an | grep 5432 commandFederico Taschin
I found the solution, and I removed the neststat output because of security. However, the issue wasn't related to it. Now I'll write the solutionFederico Taschin

1 Answers

0
votes

Found the problem: In the line

dbstudSession.setPortForwardingL(6786,"localhost",5432);

I changed "localhost" with the actual address of that server (SERVER2_DOMAIN) and everything works. I still don't really understand what the problem could have been.