pg_hba.conf 和 pg_ident.conf
admin
2023-05-21 20:22:41
0

初始化后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 -h292.168.163.102 -U postgres -d postgres -W

Password for user postgres:147258

Darren2:postgres:/usr/local/pgsql/data:>psql -h292.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 -h292.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 -h292.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


相关内容

热门资讯

新闻发布会刚结束,柳州再发地震... 中国地震台网正式测定:05月18日21时44分在广西柳州市柳南区(北纬24.37度,东经109.26...
菲律宾副总统莎拉弹劾审理程序启... 据凤凰卫视报道,菲律宾参议院5月18日下午正式组成弹劾法庭,启动对莎拉·杜特尔特的弹劾审理程序,全体...
洁丽雅报警,“私生子传闻造成严... 5月18日晚间,洁丽雅官方微博发布严正声明。声明提到,近日,网络上出现关于洁丽雅家居股份有限公司、董...
产学研共探规模化路径 量子科仪... 上证报中国证券网讯(记者 刘一枫)量子精密测量正加速跨越技术验证门槛,从“能不能用”的实验室探索,迈...
广西柳州再发生5.2级地震,震... 中国地震台网正式测定:05月18日21时44分在广西柳州市柳南区(北纬24.37度,东经109.26...
研究揭示:人们普遍认为AI比人... IT之家 5 月 18 日消息,如今,全球越来越多民众日常使用人工智能系统,尽管众多用户愿意相信人工...
红魔11S Pro正式发布:骁... 今天红魔11S Pro系列正式发布,发布后数码圈热度居高不下,不少网友直接给到电竞机皇的评价。作为常...
赖清德弹劾案19日举行投票,蓝... 海峡导报综合报道 台民意机构19日将举行赖清德弹劾案的记名投票,朝野甲级动员。中国国民党团今天(18...
小米18全员首发2nm芯片:安... 快科技5月18日消息,博主数码闲聊站最新爆料,今年下半年发布的旗舰机型中,只有小米和苹果两家的旗舰能...
文脉华章|草原文脉汇青城 多元... 央视网消息 金辉灼灼映草原,玉韵悠悠贯千年。5 月17 日,2026 年“5 ·18 国际博物馆日”...