pg_hba.conf 和 pg_ident.conf

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介: 初始化后pg_hba.conf默认的内容:# TYPE  DATABASE        USER            ADDRESS                 METHOD# "local" is for Unix domain socket connectio...

初始化后pg_hba.conf默认的内容:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only

local   all             all                                     trust

# IPv4 local connections:

host    all             all             127.0.0.1/32            trust

# IPv6 local connections:

host    all             all             ::1/128                 trust

# Allow replication connections from localhost, by a user with the

# replication privilege.

#local   replication     postgres                                trust

#host    replication     postgres        127.0.0.1/32            trust

#host    replication     postgres        ::1/128                 trust


(1)type定义了数据库的连接方式,有四种方式:

local:使用unix-domain(unix套接字)

host:使用TCP/IP连接,包括SSL和No SSL

hsotssl:使用TCP/IP连接,只能使用SSL加密方式

hostnossl:使用TCP/IP连接,不能使用SSL加密方式


(2)database指定哪些库可以被连接

all匹配所有库,要指定多个库,可以通过commas分隔


(3)user指定哪些用户可以连接

all匹配所有角色,要指定多个角色,可以通过commas分隔


(4)address指定哪些机器可以连接

首先如果type是local方式,address可以不用写;

如果type是其他格式,address可以是主机名,IP范围,IP地址

0.0.0.0/0 代表所有IP地址

172.20.143.89/32 允许这个ip登录

10.1.1.0/24 允许10.1.1.0~10.1.1.255网段登录数据库


(5)method指定客户端连接数据库认证方式


trust:只要知道数据库用户名就不需要密码或ident就能登录,建议不要在生产环境中使用。


md5:是常用的密码认证方式,如果你不使用ident,最好使用md5。密码是以md5形式传送给数据库,较安全,且不需建立同名的操作系统用户


password:以明文密码传送给数据库,建议不要在生产环境中使用


ident:

ident是Linux下PostgreSQL默认的local认证方式,凡是能正确登录操作系统用户(注:不是数据库用户)就能使用本用户映射的数据库用户不需密码登录数据库;

如果操作系统用户在pg_ident.conf文件中没有映射用户,则默认的映射数据库用户与操作系统用户同名;

用户映射文件为pg_ident.conf,这个文件记录操作系统用户和数据库用户映射关系,

比如,服务器上有名为user1的操作系统用户,同时数据库上也有同名的数据库用户,user1登录操作系统后可以直接输入psql,以user1数据库用户身份登录数据库且不需密码。


reject:拒绝认证


配置监听地址

PostgreSQL默认只监听本地端口,

[root@Darren2 postgresql-9.6.3]# netstat -nltup|grep postgres

tcp        0      0 127.0.0.1:5432              0.0.0.0:*                   LISTEN      49675/postgres      

tcp        0      0 ::1:5432                    :::*                        LISTEN      49675/postgres      

通过修改postgres.conf文件,修改监听

Darren1:postgres:/usr/local/pgsql/data:>vim postgresql.conf

#listen_addresses = 'localhost'         # what IP address(es) to listen on;

listen_addresses = '*'          # what IP address(es) to listen on;

[root@Darren2 postgresql-9.6.3]# netstat -nltup|grep postgres

tcp        0      0 0.0.0.0:5432                0.0.0.0:*                   LISTEN      50694/postgres      

tcp        0      0 :::5432                          :::*                          LISTEN      50694/postgres     


eg:

先创建一个可以登录的用户cdhu

postgres=# create role cdhu1 password '147258' login;


(1)修改pg_hba.conf,来自任何IP的客户端都可以登录,但是需要密码验证

host    all             all             0.0.0.0/0               md5

Darren2:postgres:/usr/local/pgsql/data:>pg_ctl reload

Darren2:postgres:/usr/local/pgsql/data:>psql -h192.168.163.102 -U postgres -d postgres -W

Password for user postgres:147258

Darren2:postgres:/usr/local/pgsql/data:>psql -h192.168.163.102 -U cdhu1 -d postgres -W

Password for user cdhu1:147258


(2)来自192.168.163.*的网段IP都可以登录,但是需要密码验证

host    all             all             192.168.163.0/24               md5

Darren2:postgres:/usr/local/pgsql/data:>pg_ctl reload

Darren2:postgres:/usr/local/pgsql/data:>psql -h192.168.163.102 -U cdhu1 -d postgres -W

Password for user cdhu1:147258


(3)只允许来自192.168.163.101的客户端连接数据库,但是需要密码验证

host    all             all            192.168.163.101/32               md5

Darren2:postgres:/usr/local/pgsql/data:>pg_ctl reload

#登录成功

Darren1:postgres:/usr/local/pgsql/data:>hostname -i

192.168.163.101

Darren1:postgres:/usr/local/pgsql/data:>psql -h 192.168.163.102 -U cdhu1 -d postgres -W

Password for user cdhu1:147258 可以正常登录

#登录失败

Darren2:postgres:/usr/local/pgsql/data:>hostname -i

192.168.163.102

Darren2:postgres:/usr/local/pgsql/data:>psql -h192.168.163.102 -U cdhu1 -d postgres -W

Password for user cdhu1:

FATAL:  no pg_hba.conf entry for host "192.168.163.102", user "cdhu1", database "postgres"

psql: FATAL:  no pg_hba.conf entry for host "192.168.163.102", user "cdhu1", database "postgres


(4)只允许来自192.168.163.101的客户端连接数据库,无需密码验证

host    all             all            192.168.163.101/32               trust

Darren1:postgres:/usr/local/pgsql/data:>psql -h 192.168.163.102 -U cdhu1 -d postgres


(5)如果操作系统用户在pg_ident.conf文件中没有映射用户,则默认的映射数据库用户与操作系统用户同名;

Darren2:postgres:/usr/local/pgsql/data:>vim pg_ident.conf

mapname1                cdhu1           cdhu1  (默认存在系统用户和数据库用户名相同的映射)

Darren2:postgres:/usr/local/pgsql/data:>vim pg_hba.conf

local   all             all                                     ident

[root@Darren2 postgresql-9.6.3]# useradd cdhu1

[root@Darren2 postgresql-9.6.3]# passwd cdhu1

postgres=# create role cdhu1 password '147258' login;

[root@Darren2 postgresql-9.6.3]# su - cdhu1

#系统用户cdhu1,数据库用户cdhu1,此时不需要密码即可登录数据库

[root@Darren2 postgresql-9.6.3]# su - cdhu1

[cdhu1@Darren2 ~]$ /usr/local/pgsql/bin/psql -h localhost -U cdhu1 -d postgres


相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
关系型数据库 数据库 PostgreSQL
PostgreSQL 12: Recovery.conf 文件参数合并到 postgresql.conf
PostgreSQL 12 的一个重要变化是 recovery.conf 配置文件中的参数合并到 postgresql.conf,recovery.conf 不再使用,我们看看手册的说明,如下: 发行说明 Move recovery.
4591 0
|
存储 网络协议 安全
|
关系型数据库 索引 Perl
理解 postgresql.conf 的work_mem 参数配置
主要是通过具体的实验来理解 work_mem
6294 0
|
SQL 关系型数据库 Java
【DB吐槽大会】第5期 - PG local memory
大家好,这里是DB吐槽大会,第5期 - PG local memory
|
Web App开发 关系型数据库 数据库
PostgreSQL recovery.conf 配置文件整合到 postgresql.conf
标签 PostgreSQL , recovery.conf , postgresql.conf 背景 PostgreSQL 12版本以前,配置PostgreSQL数据库恢复、流复制STANDBY,都需要配置recovery.conf,如果要修改配置,需要重启数据库。
2992 0
|
关系型数据库
PG config9.4
FILE LOCATIONS data_directory ='/pghs/pg${port}/data'hba_file ='/pghs/pg${port}/data/pg_hba.
857 0
|
网络协议 关系型数据库 网络安全
|
SQL 关系型数据库 MySQL
|
安全 关系型数据库 数据库
PostgreSQL 连接问题 FATAL: no pg_hba.conf entry for host
The server doesn't grant access to the database: the server reports FATAL: no pg_hba.conf entry for host "192.
5689 0
|
数据库 数据安全/隐私保护 安全
PostgreSQ 连接问题 FATAL: no pg_hba.conf entry for host
PostgreSQ数据库为了安全,它不会监听除本地以外的所有连接请求,当用户通过JDBC访问是,会报一些如下的异常: org.postgresql.util.PSQLException: FATAL: no pg_hba.
2043 0