码迷,mamicode.com
首页 > 其他好文 > 详细

pg_hba.conf 和 pg_ident.conf

时间:2017-10-11 01:00:27      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:pg_hba.conf 和 pg_ident.conf

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


本文出自 “10979687” 博客,请务必保留此出处http://10989687.blog.51cto.com/10979687/1971280

pg_hba.conf 和 pg_ident.conf

标签:pg_hba.conf 和 pg_ident.conf

原文地址:http://10989687.blog.51cto.com/10979687/1971280

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!