1

I have a two machines, my workstation running on Arch Linux and my cloud server running on Ubuntu.

I have installed Postgresql on my server, and it is accessible via Unix Domain Socket on the server itself, now i want to connect to the postgresql server from my workstation via ssh tunneling. I used the openssh socket tunneling to try to connect:

ssh -L/tmp/postgresql:/var/run/postgresql production@<ip>

And to connect to the server i used the following command on the psql

psql -h /tmp/postgresql

But i am getting the following error.

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

Is something i doing wrong?

Thanks

Aslam
  • 113
  • 3
  • with `psql -h` you give the __directory where it will create the socket__ while with `ssh -L` you need to give the __path of the socket__ itself, i.e. the socket inside the directory. – Steffen Ullrich Nov 24 '19 at 07:36
  • @SteffenUllrich could you give me an example – Aslam Nov 24 '19 at 08:14
  • Please have a look at the error message where the exact path is given. I.e. you have to use `/tmp/postgreql/.s.PGSQL.5432` instead of `/tmp/postgresql` in your ssh command and likely do the same for `/var/run/postgresql` to specify the socket and not directory on the server side. – Steffen Ullrich Nov 24 '19 at 08:16
  • thanks @SteffenUllrich it worked. Could you please post it as answer so i can mark it as accepted – Aslam Nov 24 '19 at 08:26

1 Answers1

1

psql -h takes the directory name where it creates the unix domain socket with a name based on some hard coded scheme, i.e. .s.PGSQL.5432 by default based on the default port 5432 when doing communication via TCP. ssh -L instead expects for both local and remote side not the directory but the exact file name of the socket, i.e. not just /tmp/postgresql but /tmp/postgreql/.s.PGSQL.5432.

Steffen Ullrich
  • 13,227
  • 27
  • 39