1 year ago

#321238

test-img

user18542516

SSH JDBC postgres timeout

I'm trying to connect to postgredb with ssh. I'm on this second day and idk where can be a problem. From Intelij modul Database--> add data source --> postgreSQL connection works. I see in logs that ssh connection is set, but there's a problem with establishing connection to db

            java.util.Properties config = new java.util.Properties();
            JSch jsch = new JSch();

            String user = "test";
            String host = "test-host";
            int port = 22;
            String privateKey = "C:\\Users\\Admin\\Documents\\Access\\private.ppk";

            jsch.addIdentity(privateKey);
            System.out.println("identity added ");
            config.put("StrictHostKeyChecking", "no");
            Session session = jsch.getSession(user, host, port);
            session.setConfig(config);
            System.out.println("session created.");
            session.setConfig(config);
             session.connect();
            System.out.println("session connected.....");


            Channel channel = session.openChannel("sftp");
            channel.setInputStream(System.in);
            channel.setOutputStream(System.out);
           channel.connect();
            System.out.println("shell channel connected....");

        Connection c = DriverManager
                    .getConnection("jdbc:postgresql://test-postgres:5432/dbName",
                            "dbUser", "dbPassword");

identity added 
session created.
session connected.....
shell channel connected....
Exception in thread "main" org.postgresql.util.PSQLException: The connection attempt failed.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:331)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:223)
    at org.postgresql.Driver.makeConnection(Driver.java:400)
    at org.postgresql.Driver.connect(Driver.java:259)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at db.configdB.main(configdB.java:42)
Caused by: java.net.SocketTimeoutException: connect timed out
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:81)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:476)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:218)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:200)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:162)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:394)
    at java.net.Socket.connect(Socket.java:606)
    at org.postgresql.core.PGStream.createSocket(PGStream.java:241)
    at org.postgresql.core.PGStream.<init>(PGStream.java:98)
    at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:109)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:235)
    ... 7 more

java

postgresql

jdbc

jsch

0 Answers

Your Answer

Accepted video resources