postgresql密码文件.pgpass

前端之家收集整理的这篇文章主要介绍了postgresql密码文件.pgpass前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

也许你不像每次在登录的时候都用过psql的-W选项为某个用户输入密码,并且你不想把此用户在pg_hba.conf文件里设置为trust信任模式。那么可以尝试下使用POSTGREsql的.pgpass文件

首先,创建一个用户test

create user test;

并且使用alter user test password 'yourpassword'命令为该用户添加密码

再在pg_hba.conf文件添加如下内容

host MysqL test xx.xx.xxx.x/0 password

(xx处为你主机的完整IP地址)

登陆一把试试

[MysqL@pttest4 ~]$ psql -h pttest4 -d MysqL -U test -W
Password for user test:
Welcome to psql 8.2.16,the Postgresql interactive terminal.

Type: /copyright for distribution terms
/h for help with sql commands
/? for help with psql commands
/g or terminate with semicolon to execute query
/q to quit

MysqL=> /q

此时使用了-W选项,系统会提示你输入密码。下面我们添加~/.pgpass试试,内容

pttest4:5432:MysqL:test:test

保存以后,登录一把试试

[MysqL@pttest4 ~]$ psql -h pttest4 -d MysqL -U test
WARNING: password file "/home/MysqL/.pgpass" has world or group read access; permission should be u=rw (0600)
Password for user test:
Welcome to psql 8.2.16,the Postgresql interactive terminal.

Type: /copyright for distribution terms
/h for help with sql commands
/? for help with psql commands
/g or terminate with semicolon to execute query
/q to quit

MysqL=> /q

这时我们可以看到,仍然会提示我们输入密码才能登录。仔细看一下WARNING,因为~/.pgpass文件的权限不正确,更改一下

[MysqL@pttest4 ~]$ chmod 0600 .pgpass
[MysqL@pttest4 ~]$ ls -al |grep .pgpass
-rw------- 1 MysqL MysqL 29 May 15 16:13 .pgpass

再次登录

[MysqL@pttest4 ~]$ psql -h pttest4 -d MysqL -U test
Welcome to psql 8.2.16,the Postgresql interactive terminal.

Type: /copyright for distribution terms
/h for help with sql commands
/? for help with psql commands
/g or terminate with semicolon to execute query
/q to quit

MysqL=>

OK,顺利登录

官方文档链接如下

http://www.postgresql.org/docs/current/static/libpq-pgpass.html

原文链接:https://www.f2er.com/postgresql/196939.html

猜你在找的Postgre SQL相关文章