When working with WSL2, it creates a virtualized network interface with its own IP range. The Windows host appears as a separate IP within this subnet (typically 172.x.x.1). This differs from WSL1's direct localhost bridging approach.
To properly connect from WSL2 to PostgreSQL on Windows:
- PostgreSQL Configuration:
# In postgresql.conf listen_addresses = '*'- Windows Firewall Rules:
netsh advfirewall firewall add rule name="PostgreSQL WSL2 Access" dir=in action=allow protocol=TCP localport=5432 remoteip=172.31.210.120- pg_hba.conf Update:
# Add this line with your WSL2 IP host all all 172.31.210.120/20 scram-sha-256Use these diagnostic commands:
# From WSL2 terminal: ping 172.31.208.1 nc -zv 172.31.208.1 5432On Windows, verify PostgreSQL is listening:
netstat -ano | findstr 5432Basic connection string:
psql -h 172.31.208.1 -U postgres -d your_databaseFor applications using connection strings:
# Python example import psycopg2 conn = psycopg2.connect( host="172.31.208.1", database="mydb", user="postgres", password="yourpassword" )You can use the Windows machine name if DNS resolution works:
# First try pinging the hostname from WSL2 ping YourWindowsHostname # Then use in connection: psql -h YourWindowsHostname -U postgresFor production workloads, consider:
- Adding the connection to /etc/hosts in WSL2
- Setting up SSH tunneling for secure connections
- Using Unix domain sockets if running PostgreSQL in WSL2
When working with WSL2, it's crucial to understand its networking behaves differently from WSL1. WSL2 runs on a lightweight virtual machine with its own virtualized network interface. The Windows host appears as a separate IP address within this virtual network.
# To identify your Windows host IP from WSL2: cat /etc/resolv.conf | grep nameserver | awk '{print $2}'
For PostgreSQL to accept remote connections, three key configuration files need attention:
# postgresql.conf (typically in C:\Program Files\PostgreSQL\13\data) listen_addresses = '*' # Allows listening on all interfaces port = 5432 # Default PostgreSQL port # pg_hba.conf (same directory) # Add this line for WSL2 access: host all all 172.31.210.120/20 scram-sha-256
The most common culprit for connection timeouts is Windows Firewall blocking the port. Here's how to add an inbound rule:
# PowerShell command to allow PostgreSQL port: New-NetFirewallRule -DisplayName "PostgreSQL WSL2 Access" -Direction Inbound -LocalPort 5432 -Protocol TCP -Action Allow
Before attempting to connect with psql, verify basic connectivity:
# From WSL2 terminal: nc -zv 172.31.208.1 5432 # Checks if port is open ping 172.31.208.1 # Basic network connectivity
Try these different connection approaches from WSL2:
# Basic connection: psql -h 172.31.208.1 -p 5432 -U postgres # With password prompt: PGPASSWORD=yourpassword psql -h 172.31.208.1 -U postgres # Specifying database: psql "host=172.31.208.1 port=5432 dbname=postgres user=postgres"
If direct connection proves problematic, set up port forwarding:
# Windows PowerShell command: netsh interface portproxy add v4tov4 listenport=5432 listenaddress=0.0.0.0 connectport=5432 connectaddress=172.31.208.1
Enable detailed PostgreSQL logging to troubleshoot connection attempts:
# In postgresql.conf: log_connections = on log_disconnections = on log_hostname = on log_line_prefix = '%m [%p] %q%u@%d ' log_statement = 'all'
Instead of using IP addresses, configure WSL2 to resolve the Windows hostname:
# In WSL2's /etc/hosts: 172.31.208.1 windows-host
Now you can connect using:
psql -h windows-host -U postgres
How to Connect to Windows Host PostgreSQL from WSL2: Networking Configuration Guide
5 views