Search code examples
postgresqlsshssh-tunnel

Connect to Postgres via ssh tunnel


I have an access to database only from remote server. That means I have ssh access to remote_server And I have psql access from remote_server to database_server I don't have ssh access from remote_server to database_server (It's Amazon RDS)

How can I connect to my database from localhost? I know that I can establish ssh tunnel from localhost to remote_server like this: ssh -fN -L 9997:localhost:9996 ubuntu@remote_server

So after this I want to connect remote_server:9996 port with database_server:5432 port. How can I establish this?


Solution

  • You just need to set the remote port as database_server:5432.

    This should do the trick:

    ssh -L 9997:database_server:5432 ubuntu@remote_server
    

    Requests to localhost:9997 will be forwarded to database_server:5432 through the tunnel on remote_server