Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

I have a remote server that I can SSH into no problem. I have PostgreSQL (ps) set up on the remote server (called 'enc') and local machine, both running on 5432 (the default). The remote server does not allow connections from outside connections (local web server goes directly to it).

I am trying to set up an SSH tunnel to make running commands on the remote ps server easier than copying files across, logging into the server and running them manually on that end.

I have tried running the following command

ssh -N -L 5555:enc:5432 enc

The tunnel appears to set up correctly but when I try to connect to the remote machine with

psql -p 5555

it says it is not working

psql: could not connect to server: No such file or directory. Is the server running locally and accepting connections on Unix domain socket "/run/postgresql/.s.PGSQL.5555"?

Is this likely a problem with settings on the tunnel or restrictive user permissions (I imagine that the error would be different if that were the case)?

share|improve this question
What OS is this? – terdon Aug 20 '13 at 12:39
Linux, specifically arch connecting to debian, both with openSSH servers – Hyposaurus Aug 20 '13 at 13:15

Try this:

ssh -N -L 5555:enc:5432 localhost

and then

lsof -i :5555

If you don't see anything there your local machine isn't listening to anything.

share|improve this answer
That seems to be working well: ssh 5941 hyposaurus 4u IPv6 51698 0t0 TCP localhost.localdomain:personal-agent (LISTEN) ssh 5941 hyposaurus 5u IPv4 51699 0t0 TCP localhost.localdomain:personal-agent (LISTEN) – Hyposaurus Aug 20 '13 at 13:10
And if you run on the final server ncat -l -p 5432 and on your end ncat <server> 5432 does it work as well? – hashier Aug 20 '13 at 14:03
Not really. On the server (different version I am guessing I have to use ncat -l 54322 since -l and -p are incompatible. That says that the address is already in use. The local version says the same. – Hyposaurus Aug 21 '13 at 8:57
Readign this… makes me think I should use ssh -N -L 5555:localhost:5432 enc but that looks to be the same end regardless, so I am a bit more confused. – Hyposaurus Aug 21 '13 at 9:00
Both should work. Once you connect to enc and there you do the forward to localhost and once you connect to localhost and do a forward to enc. I got my info from – hashier Aug 21 '13 at 12:10
up vote 1 down vote accepted

Turns out he problem was further down the line. The tunnel works correctlty however then trying to connect to postgres you have to pass the -h (even it is for localhost) otherwise it will try to connect over a unix socket (which is right there in the error) and not over tcp (which is the only way it will work with a tunnel).

share|improve this answer


netstat -anp | grep LISTEN | grep "port_number"

in your server machine and look for the ip address in which the service is running.

If it is the machine's ip address you must mention that one in the ssh -L command, else if it is the loopback address ( - ipv4 or ::1 - ipv6 ) mention that in the command.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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