Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Currently I develop on a remote dev server. I SSH into the dev server (using SSH keys) as

ssh -p 22222

Then from the dev server, I connect to the database over the local network

psql -U postgres -h psqldb -d my_database

where psqldb points to on the dev server's /etc/hosts file.

I want to start developing locally on my own computer. How do I connect to the psqldb Postgres server from my local machine by using the remote dev server as an SSH tunnel (or is there a better way to do it)?


I failed to mention that I already have Postgres installed on my local server and listening on the default port 5432.

share|improve this question

closed as off topic by EEAA, Sven, Dave M, Ward, Scott Pack Apr 14 '13 at 2:28

Questions on Server Fault are expected to relate to server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

psql is a client program. It doesn't listen, it connects... – voretaq7 Apr 13 '13 at 22:06
ssh -L localhost:5432:psqldb:5432 -p 22222

And on another terminal:

psql -h localhost -d my_database -U postgres

Simple, safe and encrypted. I don't know a better way.

share|improve this answer
Actually I already use port 5432 for my local Postgres database on my local machine. If I change the first line to ssh -L localhost:5433:psqldb:5432 -p 22222, then doing psql -h localhost -d my_database -U postgres would just try to connect to my local database. – hobbes3 Apr 13 '13 at 19:55
The psql command accepts -p to change the port. psql -p 5433 -h .... – toppledwagon Apr 13 '13 at 20:02
I'm getting an error on the first terminal: channel 3: open failed: administratively prohibited: open failed. – hobbes3 Apr 13 '13 at 21:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.