天天看點

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;

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

(3)隻允許來自192.168.163.101的用戶端連接配接資料庫,但是需要密碼驗證

host    all             all            192.168.163.101/32               md5

#登入成功

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

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

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

#系統使用者cdhu1,資料庫使用者cdhu1,此時不需要密碼即可登入資料庫

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