在 Linux 上的 PostgreSQL 中,编辑 pg_hba.conf 和 postgresql.conf 并重新启动 Postgres 会导致我的集群在重新启动时崩溃(使其“关闭”)

在 Linux 上的 PostgreSQL 中,编辑 pg_hba.conf 和 postgresql.conf 并重新启动 Postgres 会导致我的集群在重新启动时崩溃(使其“关闭”)

我的 Ubuntu 18.10 笔记本电脑上有 PostgresSQL 数据库,我想使用 Windows 10 PostgreSQL 在台式机上访问笔记本电脑数据库,因为我正在学习如何使用 Microsoft 的 Power BI,而 Power BI 仅在 Windows 上可用。

因此,我编辑了我的 postgresql.conf 文件如下:

listen_addresses = 'localhost,my_public_ip_address,my_local_ip_address'

因此,我按如下方式编辑了我的 pg_hba.conf 文件(将这些行添加到文件的最底部):

host    all             all             my_local_ip_address           md5
host    all             all             my_public_ip_address          md5

此后,我运行:

sudo /etc/init.d/postgresql restart
sudo pg_lsclusters

postgresql 服务重新启动速度快得令人难以置信,暗示存在问题,并且 lsclusters 报告:

Ver Cluster Port Status Owner    Data directory              Log file
11  main    5432 down   postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11-main.log

由于某种原因,编辑 pg_hba.conf 和 postgresql.conf 导致我的集群崩溃,并且任何尝试使用“psql postgres”或“psql -h localhost -d my_database -U my_user_name”登录都会导致:

me@me-computer_name:~$ psql postgres
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"?

一旦我使用恢复的更改恢复原始 .conf 备份并重新启动 postgresql 11,一切都会正常工作。为什么编辑这两个 .conf 文件会导致我的客户端崩溃,以及如何使我的客户端允许通过我的公共/本地 IP 从我家附近的其他计算机(并且仅限于我家)进行连接?

答案1

当 Postgres 实例无法重新启动时,您总是希望首先查看pg_lsclusters( /var/log/postgresql/postgresql-11-main.log) 中提到的日志文件。它将显示错误消息,说明服务器在重新启动时立即退出的原因。

根据所提到的更改,它可能无法监听您在 中提到的某个地址listen_addresses。这些地址必须对应于服务器上安装的网络接口,而不是预期连接的设备的远程地址。可以使用该ip a list命令列出服务器上的这些接口。

就源/目的地而言,这与相反pg_hba.conf,它可能列出允许连接的远程地址(从服务器的 POV 来看是远程的)(另外通常localhost允许本地连接)。

相关内容