gpt4 book ai didi

Postgresql 远程访问没有主机的 pg_hba.conf 条目

转载 作者:行者123 更新时间:2023-11-29 13:42:06 27 4
gpt4 key购买 nike

我正在尝试远程访问安装在 IP A.A.A.A 的服务器上的 postgresql。有两台服务器,其中一台的IP为A.A.A.A,安装了postgresql;另一个有IP B.B.B.B.

我编辑了 /etc/postgresql/10/main/pg_hba.conf 并添加了以下几行

host my_db db_user B.B.B.B/32 md5
host my_db db_user A.A.A.A/32 md5

我还编辑了 /etc/postgresql/10/main/postgresql.conf 并将 listen_addresses 设置为 '*',例如这个:

#---------------------------------------------------------------------    
---------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -

listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)

我还允许通过 ufw 中的端口 5432 进行连接:

5432                       ALLOW       Anywhere                  
5432 (v6) ALLOW Anywhere (v6)

现在,如果我在服务器 A(安装了 postgres 的地方)并执行 psql "postgresql://db_user:password@A.A.A.A:5432/my_db" 它连接正常。但是,如果我使用 IP B.B.B.B 从服务器执行此操作,我会收到此错误:

psql: FATAL:  no pg_hba.conf entry for host "B.B.B.B", user "db_user", database "my_db", SSL on
FATAL: no pg_hba.conf entry for host "B.B.B.B", user "db_user", database "my_db", SSL off

我真的不明白错误在哪里。 pg_hba.conf 中的两个条目看起来相同,但一个有效,另一个无效。

最佳答案

我在 Docker 上的 PostgreSQL 实例也遇到了类似的问题。我花了几个小时来弄清楚如何解决它。我分享我的经验,希望能帮助其他面临同样问题的人。有两个具有以下路径的 pg_hba.conf 文件:

  1. /var/lib/postgresql/data/pg_hba.conf
  2. /etc/postgresql/12/main/pg_hba.conf

我正在修改第一个,不知道第二个。我使用了一个技巧来检查我是否在处理正确的文件。我注释掉了文件的所有行并重新启动了 PostgreSQL 服务。然后我看到我仍然能够从 Docker 容器内部连接到 PostgreSQL 服务。然后我寻找另一个具有相似名称的文件,所以我找到了第二个。我修改了/etc/postgresql/12/main/pg_hba.conf,运行service postgresql restart重启了PostgreSQL,修改生效。

关于Postgresql 远程访问没有主机的 pg_hba.conf 条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53746302/

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