【问题标题】:How to connect to windows postgres Database from WSL如何从 WSL 连接到 windows postgres 数据库
【发布时间】:2019-11-11 11:13:47
【问题描述】:

我正在我的 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

【问题讨论】:

  • Postgres 是否在 WSL 内的 Windows 上运行?

标签: postgresql windows-subsystem-for-linux


【解决方案1】:

WSL2 动态分配 IP 地址,并且 IP 地址可以更改,甚至无需重新启动 Windows。因此,为了可靠地连接,我们需要:

  1. 允许 Windows 和 Postgres 接受来自 WSL2 IP 地址范围的连接(默认情况下不允许)
  2. 从 WSL2 中,确定通过 psql 连接时的 Windows/Postgresql 主机的 IP 地址(这是动态的)。我们将通过.bashrcalias 为您提供便利。

很遗憾,我找不到 WSL2 IP 地址范围的确切规范。从几次测试/重启看来,WSL2 分配的 IP 地址范围为 172.*.*.*,因此我们将在配置防火墙和 Postgres 时使用它。

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

  1. 打开Windows Defender Firewall with Advanced Security
  2. 点击New Rule...
  3. 为规则类型选择Port
  4. 选择TCP,对于Specific local ports,输入5432
  5. 选择Allow the connection。从 WSL2 连接不安全,因此不要选择安全选项
  6. 至少选择Public。也可以选择DomainPrivate。只有选择了Public,我才能连接
  7. 为规则命名,例如Postgres - connect from WSL2 并创建它
  8. 右键单击新创建的规则并选择Properties,然后单击Scope 选项卡
  9. Remote IP address 下,选择These IP addresses,然后单击Add... 并输入范围172.0.0.1172.254.254.254 并应用更改
  10. 确保规则已启用

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

假设 Windows 的 Postgresql 默认安装/设置,以下文件位于 C:\Program Files\PostgresSQL\$VERSION\data

验证postgresql.conf 有以下集合:

listen_addresses = '*'

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

更新 pg_hba.conf 以允许来自 WSL2 范围的连接,例如:

# 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

重新启动 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 $2, "   winhost"}')" | sudo tee -a /etc/hosts
fi

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

用法

psql -h winhost -p 5432 -U postgres

【讨论】:

  • 要在 PosgreSQL v13 上运行,需要进行以下更改: 1. 删除 listen_addresses = '*',因为这会阻止 postgresql 启动。 2. 在pg_hba.conf 中,我将METHOD 设置为scram-sha-256。另请注意,默认情况下 PosgreSQL 13 侦听端口 5433
  • 截至 2021 年 10 月,这可能是唯一可行的解​​决方案。仅使用 nameserver 作为主机名不会从 wsl2 连接到 Windows 中的 Postgres
【解决方案2】:

对我来说,遵循以下步骤是可行的:

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

【讨论】:

    【解决方案3】:

    在 WSL2 中你需要使用主机 IP 来连接

    获取主机IP

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

    那么您需要在“具有高级安全性的 Windows Defender 防火墙”中允许 TCP 5432 入站规则


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

    把它放在 ~/.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

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

    然后

    psql -h host -p 5432 -U postgres
    

    【讨论】:

      【解决方案4】:

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

      psql -h 127.0.0.1 -p 5432 -U postgres
      

      【讨论】:

      • 我收到了psql: error: could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? 虽然相同的命令在 Windows 控制台中运行良好...
      猜你喜欢
      • 2019-06-19
      • 2017-10-07
      • 1970-01-01
      • 1970-01-01
      • 2020-10-26
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      • 2019-07-16
      相关资源
      最近更新 更多