0

I want to ssh to a server where authentication is done via a public key

ssh ssh_username@server_ipaddress -p port number

and after this, want to access a postgresql database

psql -h host_name -U user_name database_name

How to connect using any IDE

I have tried using

sudo ssh -L 6666:127.0.0.1:3306 <your_SSH_username>@<remoteserver.com>

What another approach can I use?

gshivam63
  • 37
  • 6
  • refer: https://stackoverflow.com/questions/995944/ssh-library-for-java – Gaurav Feb 13 '18 at 14:29
  • If you want debugging help with the Jsch code, then you should post a [mcve] with a good description of the error/problem you're having. If you're asking for other tools or Java code, then your question is off-topic/too broad for SO. Please narrow down what you want to ask. – Erwin Bolwidt Feb 13 '18 at 14:29
  • Possible duplicate of [SSH library for Java](https://stackoverflow.com/questions/995944/ssh-library-for-java) – Serge Ballesta Feb 13 '18 at 14:57

2 Answers2

1

You can try to ssh to remote server this way:

ssh -L 6666:host_name:5432 ssh_username@server_ipaddress -p portnumber

and then connect to database server from your local machine:

psql -h 127.0.0.1 -p 6666 -U user_name database_name

or using Java with same connection parameters.

Ivan Mogila
  • 437
  • 2
  • 9
  • can you explain me purpose of both these steps... thanks – gshivam63 Feb 13 '18 at 19:21
  • @gshivam63 First step is a local port forwarding. It forwards any connection to local port 6666 to port 5432 on host_name through ssh. You need this step because you don't have direct access to database server host from your local machine. Second step is just for testing your connection. – Ivan Mogila Feb 14 '18 at 09:07
0
ssh ssh_hostname@ssh_ipaddress -p ssh_portnumber 

psql -h db_hostname -U db_username db_name 
password:xyz 

Solution:

Terminal

$ ssh -L 6666:db_hostname:5432 ssh_hostname@ssh_ipaddress -p ssh_portnumber

after that when ssh to server, enter command

$ psql -h db_hostname -U db_username db_name

To connect from within NetBeans use:

Netbeans->Services->Postgresql=> Connect 

using

  • DriverName: PostgreSQL
  • Host: localhost
  • Database: db_name
  • port: 6666
  • username: db_username
  • password: xyz
gshivam63
  • 37
  • 6