4

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

ssh -p 22222 user@devbox.com

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 10.0.0.202 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)?

EDIT

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

hobbes3
  • 545
  • 2
  • 9
  • 23

1 Answers1

9
ssh -L localhost:5432:psqldb:5432 -p 22222 user@devbox.com

And on another terminal:

psql -h localhost -d my_database -U postgres

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

Tometzky
  • 2,649
  • 4
  • 26
  • 32
  • 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 user@devbox.com`, 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
  • 1
    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