2

How to connect to PostgreSQL server running in WSL from pgAdmin running in Windows? If you specify localhost and port 5432, the error appears:

could not connect to server: Connection refused (0x0000274D / 10061)
Is the server running on host "localhost" (:: 1) and accepting
TCP / IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D / 10061)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP / IP connections on port 5432?
NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70
  • 1
    WSL1 or WSL2? If WSL2, try `python3 -m http.server 8087` and then browse to `localhost:8087` in Windows. Does that work? If not, you are likely running into [this problem](https://stackoverflow.com/a/66266211/11810933). – NotTheDr01ds Aug 29 '21 at 16:08

2 Answers2

3

i got the same issue with a Ubuntu WSL2 and here is how i solved it :

  • in wsl get your local ip address (ip addr and look for the adress in eth0 section after inet.). for me it was 172.18.117.222/20.
  • in the pg_hba.conf add a mask line in the IPv4 section to allow connection through the local IP address : host all all 172.18.117.222/20 md5
  • restart the postgresql server.
  • connect in PgAdmin using 172.18.117.222 instead of localhost.
  • This works, but unfortunately WSL generates a new IP address on every restart, meaning you have to re-register the server in pgAdmin every time. Quite a bummer! – mchristos Nov 01 '22 at 14:30
1

In addition to the previous answer, you may also need to change the postgresql.conf file to allow sql to listen on non-local port.

For that you need to edit postgresql.conf and change listen_addresses = 'localhost' to listen_addresses = '*'

Then restart postgresql on wsl with sudo service postgresql restart

miky
  • 97
  • 7