postgresql密码文件.pgpass

来源:互联网 发布:lcd1602液晶显示数组 编辑:程序博客网 时间:2024/05/09 20:13

也许你不像每次在登录的时候都用过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

原创粉丝点击