如何在管道的两侧使用 sudo -u
How to use sudo -u on both sides of a pipe
我正在 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'
(
如果你有很多事情要做,你可以使用 heredoc shell:
sudo -u _postgres sh <<'SCRIPT'
pg_dump mydb | psql -d mydb-testing
SCRIPT
)
我正在 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'
( 如果你有很多事情要做,你可以使用 heredoc shell:
sudo -u _postgres sh <<'SCRIPT'
pg_dump mydb | psql -d mydb-testing
SCRIPT
)