如何从 WSL 连接到 windows postgres 数据库

How to connect to windows postgres Database from WSL

我 运行 在我的 Windows 计算机上使用 Postgres 11 服务。 如何从 WSL 连接到此数据库?

当我尝试 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"

它正在尝试连接到 WSL 中的 Postgres。我不想 运行 Ubuntu Postgres 使用: sudo /etc/init.d/postgresql start

明确指定您的主机、端口和用户名 例如:

psql -h 127.0.0.1 -p 5432 -U postgres

在 WSL2 中您需要使用主机 IP 进行连接

获取主机IP

grep nameserver /etc/resolv.conf | awk '{print }'

那么你需要在 'Windows Defender Firewall with Advanced Security'

中允许 TCP 5432 入站规则

我自己做了 PS.you 仍然需要在防火墙中允许 TCP 5432

将其放入 ~/.bashrc

cat /etc/hosts | grep 172.; test $? -eq 0 && || echo -e "$(grep nameserver /etc/resolv.conf | awk '{print , " host"}')\n$(cat /etc/hosts)" | sudo tee /etc/hosts

如果之前不存在,它会将主机 IP 添加到 /etc/hosts(通常在重新启动 wsl 或计算机时发生)

然后

psql -h host -p 5432 -U postgres

对我来说,有效的方法是遵循以下步骤:

  • 更改 pg_hba.conf 以监听所有接口:host all all 0.0.0.0/0 trust
  • 为 postgresql 打开防火墙
  • 使用 /etc/hosts 中指向我主机 ip 的主机名之一。我的主机名是:host.docker.internal

WSL2 动态分配 IP 地址给 Windows 主机,IP 地址甚至可以在不重新启动 Windows 的情况下更改(请参阅下面的注释)。因此,为了可靠地连接,我们需要:

  1. 允许 Windows 和 Postgres 接受来自 WSL2 IP 地址范围的连接(默认情况下不允许)
  2. 在 WSL2 中,通过 psql 连接时确定 Windows/Postgresql 主机的 IP 地址(动态)。我们将通过 .bashrcalias.
  3. 使这变得方便

很遗憾,我找不到 WSL2 IP 地址范围的确切规范。从几个 tests/reboots 看来,WSL2 分配的 IP 地址主要在 172.*.*.* 范围内,但我偶尔会分配到 192.*.*.*,因此我们将在配置防火墙和 Postgres 时使用这些地址。

为 WSL2 IP 地址添加 Windows 防火墙入站端口规则:

  1. 打开Windows Defender Firewall with Advanced Security
  2. 点击New Rule...
  3. Select Port 规则类型
  4. Select TCP 对于 Specific local ports 输入 5432
  5. SelectAllow the connection。从 WSL2 连接不安全,所以不要 select 安全选项
  6. Select 至少 Public。 select DomainPrivate 也可以。我只能在 Public 被 selected
  7. 时连接
  8. 命名规则,例如Postgres - connect from WSL2 并创建它
  9. 右键单击新创建的规则和 select Properties 然后单击 Scope 选项卡
  10. Remote IP address、select These IP addresses 下单击 Add... 并输入范围 172.0.0.1172.254.254.254
  11. 对 IP 地址范围 192.0.0.1192.254.254.254
  12. 重复步骤 9
  13. 点击 Apply 然后 OK
  14. 确保规则已启用

将 Postgres 配置为接受来自 WSL2 IP 地址的连接

假设 Windows 的默认 install/setup Postgresql 以下文件位于 C:\Program Files\PostgresSQL$VERSION\data

验证 postgresql.conf 具有以下集合:

listen_addresses = '*'

这应该已经设置为 '*' 所以这里什么都不做。

更新 pg_hba.conf 以允许来自 WSL2 范围的连接,例如对于 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

对于 Postgresql 13+,您应该使用 scram-sha-256 作为方法。

重新启动 Postgres 以使更改生效。这可以通过 Windows Services 应用程序或具有管理员权限的 cmd 来完成,例如对于 Postgresql 12:

net stop postgresql-x64-12
net start postgresql-x64-12

WSL Shell 便利设施

在 WSL 中,将以下内容添加到您的 ~/.bashrc 或类似内容:

# 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 , "   winhost"}')" | sudo tee -a /etc/hosts
fi

然后重新加载您的 .bashrc 更改:source ~/.bashrc

用法

psql -h winhost -p 5432 -U postgres

备注:

  • WSL2 分配给 Windows 主机的 IP 地址与分配给网络上物理 Windows 机器的 IP 地址不同。 WSL2 使用 vEthernet 个连接。
  • 您可以通过 Control Panel\Network and Internet\Network Connections
  • 检查 vEthernet 连接
  • 请注意,在查看 IPv4 属性时,IP 地址看起来好像是静态设置的,但事实并非如此!尝试重新启动并再次检查 IPv4 属性
  • 如果有一天您无法连接到 Postgres,请检查 winhost 是否在每个防火墙规则的 IP 地址范围内。可能是 WSL 分配了一个我们没有预料到的 IP 地址!

可以通过两种可能的方式解决此问题

明确指定主机名和用户名

psql -h localhost -U postgres

导航到 runpsql.sh 文件和 运行 查询

/Library/PostgreSQL/14/scripts/runpsql.sh

现在 运行 通过输入密码进行 psql 查询(如果需要)