简体   繁体   中英

Postgresql remote access no pg_hba.conf entry for host

I'm trying to get remote access to postgresql, installed on a server with IP AAAA There are two servers which one them has IP AAAA, where postgresql has been installed on; and the other one has IP BBBB

I've edited /etc/postgresql/10/main/pg_hba.conf and added the following lines

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

I've also edited /etc/postgresql/10/main/postgresql.conf and set listen_addresses to '*' , like this:

#---------------------------------------------------------------------    
---------
# 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)

I have also allowed connections through port 5432 in ufw:

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

Now, if I am in server A (where postgres is installed) and do psql "postgresql://db_user:password@AAAA:5432/my_db" it connects fine. But if I do it from the server with IP BBBB I get this error:

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

I really don't understand where the mistake is. Both entries in pg_hba.conf look the same, but one works and the other doesn't.

I had a similar problem with my PostgreSQL instance on Docker. I spent hours to figure out how to solve it. I am sharing my experience hoping to help others that face the same problem. There are two pg_hba.conf files with following paths:

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

I was modifying the first one and was not aware of the second one. I used a trick to check if I was working on the right file. I commented out all lines of the file and restarted PostgreSQL service. Then I saw that I was still able to connect to the PostgreSQL service from inside of Docker container. Then I looked for another file with a similar name, so I reached the second one. I modified /etc/postgresql/12/main/pg_hba.conf , restarted PostgreSQL by running service postgresql restart , and changes took effect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM