版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_29622761/article/details/73130102
1、测试需求
192.168.181.141上的postgresql数据库,192.168.1.138要无密码登录进来,192.168.181.181要md5登录
2、步骤
1)检查192.168.181.141上的postgresql数据库的postgres用户l密码
postgres=# select pg_user.usename,pg_user.passwd from pg_user;
usename | passwd
----------+----------
postgres | ********
(1 row)
postgres=# select usename,passwd from pg_shadow;
usename | passwd
----------+--------
postgres |
(1 row)
postgres=# select md5('');
md5
----------------------------------
d41d8cd98f00b204e9800998ecf8427e
(1 row)
postgres=# select rolname,rolpassword from pg_authid;
rolname | rolpassword
-------------------+-------------
postgres |
pg_signal_backend |
(2 rows)
postgres=# select rolname,rolpassword from pg_authid;
rolname | rolpassword
-------------------+-------------
postgres |
pg_signal_backend |
(2 rows)
2)修改密码为postgres
postgres=# ALTER USER postgres WITH PASSWORD 'postgres';
ALTER ROLE
3)配置192.168.181.141的pg_hba.conf文件。
host all all 192.168.181.180/32 md5
host all all 192.168.181.138/32 trust
备注:例如如下的配置,会按顺序从上往下执行。
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host replication postgres 192.168.1.145/32 trust
host replication postgres 192.168.1.146/32 trust
host all postgres 192.168.1.0/32 md5
host all all 0.0.0.0/0 md5
0.0.0.0/32表示 允许地址在0.0.0.0-255.255.255.255范围内的客户端
192.168.1.0/24表示子网掩码为255.255.255.0,此时允许192.168.1.0-192.168.1.255范围内的客户端地址链接。
192.168.1.0/32表示IP为192.168.1.0
4)测试
pg961为192.168.181.180
[postgres@pg961 opt]$ psql -h 192.168.181.141 -p 5432 -U postgres
Password for user postgres:
psql (9.6.1)
Type "help" for help.
postgres=#
zabbix为192.168.181.138
[postgres@zabbix /]$psql -h 192.168.181.141 -p 5432
psql (9.5.4, 服务器 9.6.1)
警告:psql 主版本9.5,服务器主版本为9.6.
一些psql功能可能无法正常使用。
输入 "help" 来获取帮助信息.
postgres=
5)附加测试:配置.pgpass
因为从192.168.181.180登录到192.168.181.141的数据库中,每次要输入密码。那么可以在/home/postgre/目录下建立一个文件
格式:hostname:port:database:username:password
[postgres@pg961 opt]$ vim /home/postgres/.pgpass
192.168.181.141:5432:postgres:postgres:postgres
再次进行登录就可以不用密码了。
[postgres@pg961 opt]$ psql -h 192.168.181.141 -p 5432 -U postgres
psql (9.6.1)
Type "help" for help.
postgres=#
测试完成