温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

pg_hba.conf 和 pg_ident.conf

发布时间:2020-06-02 20:37:18 来源:网络 阅读:920 作者:Darren_Chen 栏目:数据库

初始化后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


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI