Federico Taschin
Federico Taschin

Reputation: 2195

FATAL: no pg_hba.conf entry for host "::1" trying to connect to postgresql server over ssh tunnel

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.

Upvotes: 0

Views: 1399

Answers (1)

Federico Taschin
Federico Taschin

Reputation: 2195

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.

Upvotes: 0

Related Questions