Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 897
Snobby Avatar asked May 22 '26 17:05

Snobby


1 Answers

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

like image 89
cogitoergosum Avatar answered May 24 '26 16:05

cogitoergosum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!