0

I run Postgres 12 service in Windows Subsystem for Linux 2 (WSL2).

In my /etc/postgresql/12/main/postgresql.conf, I have:

listen_addresses = '*'

while my /etc/postgresql/12/main/pg_hba.conf has this content:

local all postgres peer
local all all peer
host all all 0.0.0.0/0 md5
host all all ::0/0 md5
host all all 0.0.0.0/0 md5
host all all ::0/0 md5
host all all all md5

If I try to connect to this server from any other device then the local machine, it times out.

Does anyone have any idea why should this be?

asked Mar 22, 2021 at 1:26
5
  • That's a firewall. You are not planning to use that platform for anything serious where you would mind losing data, right? Commented Mar 22, 2021 at 6:18
  • yep, just for home testing Commented Mar 22, 2021 at 16:45
  • Good, because as far as I know it is not safe for databases. Commented Mar 22, 2021 at 17:10
  • I additionally opened the port 5432 in windows firewall, but same result Commented Mar 22, 2021 at 17:16
  • 1
    If the connection times out, that is a network problem and has nothing to do with the database. Commented Mar 22, 2021 at 17:21

1 Answer 1

1

You need to forward Windows port to WSL2.

When using a WSL 1 distribution, if your computer was set up to be accessed by your LAN, then applications run in WSL could be accessed on your LAN as well.

This isn't the default case in WSL 2. WSL 2 has a virtualized ethernet adapter with its own unique IP address. Currently, to enable this workflow you will need to go through the same steps as you would for a regular virtual machine. (We are looking into ways to improve this experience.)

Here's an example PowerShell command to add a port proxy that listens on port 4000 on the host and connects it to port 4000 to the WSL 2 VM with IP address 192.168.101.100.

netsh interface portproxy add v4tov4 listenport=4000 listenaddress=0.0.0.0 connectport=4000 connectaddress=192.168.101.100

Reference : Comparing WSL 1 and WSL 2 : Accessing a WSL 2 distribution from your local area network (LAN) (Microsoft | Docs)

John K. N.
18.9k14 gold badges56 silver badges117 bronze badges
answered Jul 2, 2021 at 7:17
2
  • 1
    Welcome to DBA.SE. Always consider adding the relevant information, as link only answers are normally deleted. Links tend to expire and then the answer becomes a non-answer. Thanks. Commented Jul 2, 2021 at 9:55
  • Just in case someone else was looking for a current link to the same information: learn.microsoft.com/en-us/windows/wsl/… Commented Jul 16, 2022 at 3:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.