以下是我设置.pgpass文件用到的文档
The following example assumes that your assigned PostgreSQL server is psql0 and your account name
is “alice”.
$ bash psql0.sh
$ source ∼/.bash profile
$ bash check.sh
PGUSER=alice
PGHOST=psql0 PGDATABASE=database
PGSQL EDITOR=/usr/local/bin/nano $
The configuration using psql0.sh/psql1.sh needs to be performed only once. You can now start psql with simply “psql”.
Finally, to avoid having to enter your account password when starting psql, create a password file named .pgpass in your home directory as follows (assuming that your assigned PostgreSQL server is psql0, your account name is “alice” and your password is “wonderland”).
$ echo "psql0:*:database:alice:wonderland" > ∼/.pgpass
$ chmod 600 ∼/.pgpass #这条命令是在登录时自动去指定文件中找密码
$
The .pgpass file will need to be edited whenever you change your password using the alter command.
照着文档想不输入密码进去psql,但是我的密码最后一个字符是!
,比如密码是 “wonderland!”
代码是这样的:
$ echo "psql0:*:database:alice:wonderland!" > ∼/.pgpass
导致出现这样的提示:
-bash: !” event not found
然后很作死的在!
后面加了一个空格,代码变成了这样
$ echo "psql0:*:database:alice:wonderland! " > ∼/.pgpass
结果就是密码 "wonderland! "
成功写入了.pgpass文件,但是我进不去psql了,因为正确的密码是"wonderland!"
(请注意空格!!!)
解决方案
- 1 修改.pgpass文件 -> 失败
试图直接重写.pgpass文件,就是直接再次执行echo命令,但是提示 cannot overwrite this file (大概是这个意思)
可能是因为chmod设置的权限问题,把600改成了666,参照如下:
Examples:
chmod 600 file – owner can read and write
chmod 700 file – owner can read, write and execute
chmod 666 file – all can read and write
chmod 777 file – all can read, write and execute
然后再次执行echo命令,多了一个提示:.pgpass文件的权限应该要更高才行,但是依然cannot overwrite
- 2 把密码修改为
"wonderland! "
(有空格)
前面说到chrom 600的目的就是登录的时候直接自动去文件中找密码,不需要用户输入,这样如果.pgpass文件中的密码错误,就没有办法登录。
但是登录的时候强制要求需要用户输入密码:
$psql -U alice -d database -h psql0 -w
这时就会提示输入密码,输入正确的密码"wonderland!"
,进入pgpass,接下来把密码修改为写在.pgpass中的密码"wonderland! "
:(相当于被迫改密码)
database=> alter user alice password ’wonderland! ’;
这样以后连上服务器,就可以直接用psql
命令进入
- 3 还么设置.pgpass文件用户,需要解决累死
!"
event not found 的问题可以参考下面这篇文章,很全面。
比如这里在设置密码前就可以使用
set+H
这样就不会出现!"
event not found 的情况了
未解决
1、不知道怎么修改.pgpass文件啊
2、.pgpass文件是系统中的唯一的文件吗?如何把其他文件设置为查找密码的入口?