Postgresql远程访问主机没有pg_hba.conf项



我正在尝试远程访问postgresql,它安装在具有IP a.a.a.a.的服务器上。有两台服务器,其中一台具有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,更改生效了。

最新更新