PostgreSQL访问控制,允许&禁止指定IP访问

PostgreSQL中允许哪些IP的机器能够访问数据库是由配置文件pg_hba.conf控制的,通过配置该文件,能够指定哪些ip可以访问,哪些ip不可以访问,以及访问的认证方式。hba是host-based authentication的缩写。

原文地址:

https://mytecdb.com/blogDetail.php?id=77

initdb在初始化数据目录的时候,会生成一个默认的pg_hba.conf文件。pg_hba.conf文件格式由多条记录组成,每条记录占一行,#开头为注释,这些记录控制着客户端访问数据库的权限和认证方式。

先看一个PostgreSQL默认生成的pg_hba.conf文件示例,如下:

# TYPE DATABASE USER ADDRESS METHOD 1. "local" is for Unix domain socket connections only local all all trust 1. IPv4 local connections: host all all 127.0.0.1/32 trust 1. IPv6 local connections: host all all ::1/128 trust 1. Allow replication connections from localhost, by a user with the 1. replication privilege. local replication all trust host replication all 127.0.0.1/32 trust host replication all ::1/128 trust