8

I'm running Ubuntu WSL 2 on Windows 10. Before I installed WSL I already had Postgresql installed on my Windows 10. I wanted to connect to the database from WSL but so far it fails.

When running:

psql

I get:

psql: error: could not connect to server: Connection refused
    Is the server running on host "127.0.0.1" and accepting
    TCP/IP connections on port 5432?

When running:

psql -h 127.0.0.1 -p 5432 -U postgres

I get:

psql: error: could not connect to server: Connection refused
    Is the server running on host "127.0.0.1" and accepting
    TCP/IP connections on port 5432?

And WSL clearly doesn't see the database. When running this:

sudo service postgresql start

It returns:

postgresql: unrecognized service

I know that one solution is to install the database in WSL but wanted to try to connect to existing instance first.

6
  • Use the local network address as opposed to the loopback address when using psql -h 127.0.0.1 -p 5432 -U postgres Commented Jan 12, 2021 at 9:46
  • @RamanSailopal I think I tried that too. I extracted the address using grep nameserver /etc/resolv.conf | awk '{print $2}' Commented Jan 12, 2021 at 11:28
  • It should be one of the addresses shown by executing "ip add show" Commented Jan 12, 2021 at 11:36
  • Tried that just now. Same issue... Commented Jan 12, 2021 at 11:39
  • 2
    Did you ever solve this? Having the same issue today. Yesterday it was working fine, today I can't connect. Although I don't get the error message on the service start, it just accepts the command. Commented Apr 7, 2021 at 16:06

1 Answer 1

12

You should edit pg_hba.conf

  1. check wsl subnet
    execute ipconfig from cmd. Below is example.
Ethernet Adapter vEthernet (WSL)
   IPv4 Address . . . . . . . . . . . .: 172.19.230.81
  1. edit pg_hba.conf
    pg_hba.conf path is C:\Program Files\PostgreSQL\{postgresqlVersion}\data\pg_hba.conf. if you installed Windows Postgresql default install path.
    subnet mask is use value from ipconfig
host    all     all     172.19.230.0/24 md5
  1. restart postgresql you can restart postgresql from services.msc. service name is postgresql-x64-{postgresqlVersion}.

  2. access from psql

psql -h 172.19.230.81 -p 5432 -U postgres
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.