0

Recently I've updated my Linux distro to wsl2. An error appeared, so my rails app couldn't connect to PostgreSQL. I followed the instructions from this article, and everything is working fine until I reboot my computer. After reboot, cat /etc/resolv.conf command displays a different IP address under the nameserver term. It appears that every time after I reboot/shutdown my computer, I'll have to change pg_hba.conf. Is there any way to configure wsl2 to have a stable nameserver so there'll be no need to change pg_hba.conf every time? Thanks.

Alex Zakruzhetskyi
  • 1,383
  • 2
  • 22
  • 43

1 Answers1

1

Inspired by this post. This is what I have done to resolve the issue:

1. Turn off generation of /etc/resolv.conf

Using your Linux prompt, (I'm using Ubuntu), modify (or create) /etc/wsl.conf with the following content

[network]
generateResolvConf = false

2. Restart the WSL2 Virtual Machine

Exit all of your Linux prompts and run the following Powershell command

wsl --shutdown

3. Create a custom /etc/resolv.conf

Open a new Linux prompt and cd to /etc

If resolv.conf is soft linked to another file, remove the link with

rm resolv.conf

Create a new resolv.conf with the following content

nameserver 172.20.128.1

4. Restart the WSL2 Virtual Machine

5. Edit pg_hba.conf in C:\Program Files\PostgreSQL\pg_version\data

TYPE    DATABASE        USER            ADDRESS                 METHOD
host    all             all             172.20.0.1/16           md5

So, in this way, there is no need to edit pg_hba.conf every time after reboot.

Alex Zakruzhetskyi
  • 1,383
  • 2
  • 22
  • 43
  • 3
    **WARNING**: This will break a lot of things. Essentially, you will no longer be able to use the internet within the WSL after setting the nameserver to your local server – Martin Thoma Sep 22 '20 at 06:53