2

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 Answer 1

9

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 the ping $(hostname).local again. Delete the rule with Remove-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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. 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 "<ip address>", user "<user>", database "<database name>", 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?

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.