在Mac上通过网络访问postgresql服务器

Sud*_*dar 2 postgresql macos

我在Mac上安装了Postgres App并运行了一个数据库.

我可以使用命令从终端连接它 psql -h localhost

现在我想从同一网络上的另一台机器访问此服务器.

当我psql -h <hostname> -U <username>从另一台机器上做的时候,我得到了错误

psql: could not connect to server: Connection refused
    Is the server running on host "my hostname" (xx.xx.xx.xxx) and accepting
    TCP/IP connections on port 5432?
Run Code Online (Sandbox Code Playgroud)

在我运行服务器的机器中,lsof -i | grep LISTEN我得到以下结果.

postgres  3196 sudarm    5u  IPv6 0x1caf6120      0t0  TCP localhost:5432 (LISTEN)
postgres  3196 sudarm    6u  IPv4 0x14678db0      0t0  TCP localhost:5432 (LISTEN)
postgres  3196 sudarm    7u  IPv6 0x1caf6a80      0t0  TCP localhost:5432 (LISTEN)
Run Code Online (Sandbox Code Playgroud)

我是否还需要做其他事情才能从另一台机器连接到服务器?

wil*_*ynn 10

错误消息询问正确的问题:服务器是否接受端口5432上的连接?lsof您提供的输出表明不,不是.PostgreSQL正在监听localhost:5432,这意味着它只接受来自数据库服务器本身的连接.

打开服务器postgresql.conf,设置listen_addresses = '*'并重启PostgreSQL.然后它将监听所有接口上的连接,从而接受通过网络的连接.

从这里开始,您可能遇到的下一个问题是身份验证.(PostgreSQL会接受连接,但是你可能没有告诉它一旦有来自网络的连接就该做什么.)确保服务器pg_hba.conf有一个与你的数据库/用户/源地址组合相匹配的条目 - 这host all all 10.0.0.0/8 md5可能是合适的 - 并根据需要重新加载PostgreSQL以应用更改.

md5部分告诉PostgreSQL使用密码尝试身份验证,因此如果您还没有密码,则还需要为相关用户设置密码.但是,您通常会管理数据库(例如psql template1,在数据库服务器上)ALTER USER whomever WITH PASSWORD 'your_new_password'.

  • psql中的@Sudar`SHOW config_file;`和`SHOW hba_file;`是Pg运行时最简单的方法。 (2认同)