gpt4 book ai didi

linux - 如何在管道的两侧使用 sudo -u

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:44:17 25 4
gpt4 key购买 nike

我正在 OS X 上做一些 PostgreSQL 管理工作。有时我需要将一个数据库复制到另一个数据库,我是这样做的。

sudo -u _postgres pg_dump mydb > mydb.sql
sudo -u _postgres psql -d mydb-testing -f mydb.sql

但我宁愿只是这样做

sudo -u _postgres pg_dump mydb | sudo -u _postgres psql -d mydb-testing

然而,当我这样做时,系统会要求我输入密码,但总是以不正确为由拒绝。

我正在寻找一种解决方案,我可以在管道的两侧以 _postgres 用户身份运行命令。

最佳答案

我相信

sudo -u _postgres pg_dump mydb | sudo -u _postgres psql -d mydb-testing

只要您确保 sudo 不会同时从您的终端询问您的密码,就应该可以工作。

如果我缓存我的 sudo 密码,我可以正常地从一个 sudo 指令到另一个指令:

sudo -u root echo hello world | sudo -u root tr a-z A-Z #prints HELLO WORLD

另一种选择是使用一个 sudo 生成一个 shell:

sudo -u _postgres sh -c 'pg_dump mydb | psql -d mydb-testing'

(如果您在该 shell 中有很多事情要做,则可以使用 heredoc:

sudo -u _postgres sh <<'SCRIPT' 
pg_dump mydb | psql -d mydb-testing
SCRIPT

)

关于linux - 如何在管道的两侧使用 sudo -u,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38964715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com