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, 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.

  • The error message clearly indicates a connectivity problem between ssh server and postgres server. Yet it seems you are running both on the same host. Are you sure you are specifying the same IP in both locations in that ssh command? – kasperd Feb 1 '16 at 17:43

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.

  • you are welcome. Glad to help. – Jakuje Feb 1 '16 at 19:00
  • This answer cannot explain the symptoms described in the question. I already tested the scenario described in this answer, and it does not trigger a timeout. Rather it cause the connection to fail instantly with open failed: connect failed: Connection refused. – kasperd Feb 2 '16 at 12:38
  • It depends on your configuration. Basically, you might have ExitOnForwardFailure option, which would cause your behaviour or not and it is silently ignored, as far as I know. – Jakuje Feb 2 '16 at 12:40

I was having a very similar issue (Ubuntu here): ssh tunnel for PostgreSQL's port, psql acessing remote database using that tunnel. When I requested an autocomplete on psql (select * from x), the tunnel would hang/crash.

Solution: change the MTU for all interfaces from 1500 to 576. Explanation: http://www.snailbook.com/faq/mtu-mismatch.auto.html

There went a few hours of my life..

  • That is terrible advice. – womble Dec 28 '18 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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