psql - postgres 补bug

本文档详细介绍了在配置.pgpass文件过程中遇到的特殊字符问题,特别是感叹号(!)的处理方式。通过调整文件权限、修改密码及使用特定命令避免错误,最终解决了登录PostgreSQL时的认证难题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下是我设置.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 的问题可以参考下面这篇文章,很全面。

http://bbs.chinaunix.net/thread-2017791-1-1.html

比如这里在设置密码前就可以使用

set+H

这样就不会出现!"event not found 的情况了

未解决

1、不知道怎么修改.pgpass文件啊
2、.pgpass文件是系统中的唯一的文件吗?如何把其他文件设置为查找密码的入口?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值