Timothy Oliver
Timothy Oliver

Reputation: 540

Connecting to Postgres running on Windows from django running on WSL

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.

Upvotes: 2

Views: 2220

Answers (1)

NotTheDr01ds
NotTheDr01ds

Reputation: 20629

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.

Upvotes: 9

Related Questions