gpt4 book ai didi

postgresql - 从 WSL 2 连接到现有的 Postgresql 实例

转载 作者:行者123 更新时间:2023-12-04 08:16:54 65 4
gpt4 key购买 nike

我在 Windows 10 上运行 Ubuntu WSL 2。在安装 WSL 之前,我已经在 Windows 10 上安装了 Postgresql。我想从 WSL 连接到数据库,但到目前为止它失败了。
运行时:

psql
我得到:
psql: error: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
运行时:
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 "127.0.0.1" and accepting
TCP/IP connections on port 5432?
WSL 显然没有看到数据库。运行时:
sudo service postgresql start
它返回:
postgresql: unrecognized service
我知道一种解决方案是在 WSL 中安装数据库,但想先尝试连接到现有实例。

最佳答案

您应该编辑 pg_hba.conf

  • 检查 wsl 子网
    执行 ipconfig从 cmd。下面是例子。
  • Ethernet Adapter vEthernet (WSL)
    IPv4 Address . . . . . . . . . . . .: 172.19.230.81
  • 编辑 pg_hba.confpg_hba.conf路径是 C:\Program Files\PostgreSQL\{postgresqlVersion}\data\pg_hba.conf .如果您安装了 Windows Postgresql 默认安装路径。
    子网掩码是来自 ipconfig 的使用值
  • host    all     all     172.168.140.0/24    md5
  • 重启 postgresql
    您可以从 services.msc 重新启动 postgresql .服务名称是 postgresql-x64-{postgresqlVersion} .
  • 从 psql 访问
  • psql -h 172.19.230.81 -p 5432 -U postgres

    关于postgresql - 从 WSL 2 连接到现有的 Postgresql 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65667037/

    65 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com