I'm running Postgres 11 service on my Windows computer. How can I connect to this database from WSL?
When I try su - postgres
:
postgres@LAPTOP-NQ52TKOG:~$ psql
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"
It's trying to connect to a Postgres in WSL. I don't want to run Ubuntu Postgres using:
sudo /etc/init.d/postgresql start
WSL2 assigns IP address to the Windows host dynamically and the IP addresses can change without even rebooting Windows (see Notes below). So to reliably connect we'll need to:
psql
. We'll make this convenient via .bashrc
and alias
.Unfortunately I couldn't find the exact specification for the WSL2 IP address range. From several tests/reboots it appears that WSL2 is assigning IP addresses primarily in range of 172.*.*.*
but I have occasionally been assigned 192.*.*.*
so we'll use these when configuring the firewall and Postgres.
Add Windows Firewall Inbound Port Rule for WSL2 IP Addresses:
Windows Defender Firewall with Advanced Security
New Rule...
Port
for rule typeTCP
and for Specific local ports
enter 5432
Allow the connection
. Connecting from WSL2 won't be secure so don't select the secure optionPublic
. Can select Domain
and Private
as well. I could only connect if Public
was selectedPostgres - connect from WSL2
and create itProperties
then click on the Scope
tabRemote IP address
, select These IP addresses
then click Add...
and enter range 172.0.0.1
to 172.254.254.254
192.0.0.1
to 192.254.254.254
Apply
then OK
Configure Postgres to Accept Connections from WSL2 IP Addresses
Assuming a default install/setup of Postgresql for Windows the following files are located under C:\Program Files\PostgresSQL\$VERSION\data
Verify that postgresql.conf
has following set:
listen_addresses = '*'
This should already be set to '*'
so nothing do here.
Update pg_hba.conf
to allow connections from WSL2 range e.g. for Postgresl 12:
# TYPE DATABASE USER ADDRESS METHOD
# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all 172.0.0.0/8 md5
host all all 192.0.0.0/8 md5
For Postgresql 13+ you should use scram-sha-256
as the method.
Restart Postgres for changes to take effect. This can be done either from the Windows Services
app or from cmd
with Administrator privileges e.g. for Postgresql 12:
net stop postgresql-x64-12
net start postgresql-x64-12
WSL Shell Conveniences
In WSL, add following to your ~/.bashrc
or similar:
# Add DNS entry for Windows host
if ! $(cat /etc/hosts | grep -q 'winhost'); then
echo 'Adding DNS entry for Windows host in /etc/hosts'
echo '\n# Windows host - added via ~/.bashhrc' | sudo tee -a /etc/hosts
echo -e "$(grep nameserver /etc/resolv.conf | awk '{print $2, " winhost"}')" | sudo tee -a /etc/hosts
fi
Then reload your .bashrc
changes: source ~/.bashrc
Usage
psql -h winhost -p 5432 -U postgres
Notes:
vEthernet
connections.vEthernet
connections via Control Panel\Network and Internet\Network Connections
winhost
is in the IP address range per firewall rules. Could be WSL has assigned an IP address that we weren't expecting!In WSL2 you need to use host IP to connect
to get host IP
grep nameserver /etc/resolv.conf | awk '{print $2}'
then you need to allow TCP 5432 inbound Rules in 'Windows Defender Firewall with Advanced Security'
I made my self PS.you still need to allow TCP 5432 in Firewall
put this in ~/.bashrc
cat /etc/hosts | grep 172.; test $? -eq 0 && $1 || echo -e "$(grep nameserver /etc/resolv.conf | awk '{print $2, " host"}')\n$(cat /etc/hosts)" | sudo tee /etc/hosts
its append host IP to /etc/hosts if not exist before(usually happened when restart wsl or computer)
then
psql -h host -p 5432 -U postgres
Specify your host, port, and username explicitly For example:
psql -h 127.0.0.1 -p 5432 -U postgres
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With