Ssh – Connection to postgresql hangs over SSH tunnel on OSX

mac-osxpostgresqlsshssh-tunnel

Trying to connect to a remote PG server on OSX but it keeps timing out.

First, I set up the tunnel:

ssh -L 5433:serverip:5432 user@serverip

Then in a separate terminal I attempt to connect to PG:

psql -h localhost -p 5433 -U user

Here the connection just sits for about 3 minutes. Then I get the following message:

psql: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.

On the ssh tunnel terminal, all I see is this:

channel 3: new [direct-tcpip]
(3 mins later:)channel 3: open failed: connect failed: Connection timed out

I've tried every variation I can think of (localhost vs 127.0.0.1, diff usernames). Nothing is working. This exact setup works fine on Windows Putty, so its not the server. I am also able to ssh and run psql on the server, so it's not PG.

Best Answer

I believe you have postgresql configured to listen only on loopbavk (localhost). You should use the forwarding like this:

ssh -L 5433:localhost:5432 user@serverip

Otherwise it will try to connect to public IP of your server, where is nothing listening or nothing authorized to login.