I primarily develop on windows but I'm testing my app using WSL since I will push it to a linux server. My current issue is connecting the django app, running on WSL to postgres, running on windows. Many articles explain how to connect to postgres running on WSL but not the other way round.
1 Answers
I haven't been able to test all of this, so we may need to tweak a few items in this answer, but here are my recommended steps:
First, assuming this is a WSL2 instance, mDNS should work for accessing the Windows host IP from WSL. Try
ping $(hostname).local
from inside WSL for starters.If this resolves an IP address, then that's step 1. If not, then there are alternatives (see this answer).
I'm assuming the ping won't return due to the firewall. You'll need a rule to enable it. From an admin PowerShell,
New-NetFirewallRule -DisplayName "WSL Ping" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" -Action Allow -Protocol ICMPv4
. Then try theping $(hostname).local
again. Delete the rule withRemove-NetFirewallRule -DisplayName "WSL Ping"
If that works, then you really have your answer on how to access PostgreSQL -- Set up a firewall rule on that interface. It should be something like
New-NetFirewallRule -DisplayName "WSL PostgreSQL" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" -Action Allow -Protocol TCP -LocalPort 5432
.Then you should be able to access it from the Django app in WSL using the {computername}.local address.

- 15,620
- 5
- 44
- 70
-
2Thanks. This worked. However, I had to make some additional steps. I had to change the host database IP in django to the resolved IP. I also got a `django.db.utils.OperationalError: FATAL: no pg_hba.conf entry for host "
", user " – Timothy Oliver May 13 '21 at 21:56", database " ", SSL off`. I solved this by adding the resolved IP address to pg_hba.conf. -
Cool - Good to hear. Just curious for future reference -- Are you saying the mDNS name didn't work, so you had to resort to IP instead for `pg_hab.conf` and the Django settings? – NotTheDr01ds May 14 '21 at 00:56