初始化后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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。