加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

一个bash脚本来改变postgresql用户密码

发布时间:2020-12-15 18:56:13 所属栏目:安全 来源:网络整理
导读:我可以这样改变postgresql用户密码(2步): $su - postgres -c 'psql -U postgres -d postgres'# Alter user postgres with password 'password'; 现在我想使用单行命令(1步)来更改密码,如: su - postgres -c 'psql -U postgres -d postgres -c "alter user
我可以这样改变postgresql用户密码(2步):
$su - postgres -c 'psql -U postgres -d postgres'
# Alter user postgres with password 'password';

现在我想使用单行命令(1步)来更改密码,如:

su - postgres -c 'psql -U postgres -d postgres -c "alter user postgres with password ''password'';"'

我听说使用双引号排除一个单引号,所以我添加了双引号.但是显示错误信息:

ERROR:  syntax error at or near "password"
LINE 1: alter user postgres with password password;

有人可以让我知道如何使用一行命令来做到这一点吗?

如果您使用sudo更容易:
sudo -u postgres psql -U postgres -d postgres -c "alter user postgres with password 'password';"

但是也可以与su一起工作:

su - postgres -c "psql -U postgres -d postgres -c "alter user postgres with password 'password';""

我使用外部双引号并转义内部双引号,以便它们作为单个调用参数的一部分传递给su,并由shell取消转义,因此实际查询文本将作为单个参数传递给psql,包括单引号密码.

sudo的原因之一是更容易,因为它使用更智能的方式执行子进程,而不是运行第二个shell实例来执行它.你需要一个更少的shell元字符转义的层.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读