Postgresql 无法使用 -u 快捷方式更改为 root

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35782366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:11:20  来源:igfitidea点击:

Postgresql cannot change to root with -u shortcut

linuxpostgresql

提问by Johnathan

Recently updated from Postgresql 9.1to 9.3.

最近从 Postgresql 9.1更新到9.3

Everything works fine, but I noticed now when I type in:

一切正常,但我现在输入时注意到:

sudo -u postgres psql

I am getting hit with a permission denied errorfor changing dir to root.

我因将 dir 更改为 root而遇到权限被拒绝错误

"Could not change directory to /home/root.

“无法将目录更改为 /home/root。

However, when I use:

但是,当我使用:

sudo su - postgres 
psql

It accesses it fine. How can I fix this?

它可以很好地访问它。我怎样才能解决这个问题?

回答by Kirk Roybal

change directory to someplace that postgres has access to:

将目录更改为 postgres 可以访问的地方:

cd /tmp
sudo -u postgres psql

回答by DylanYoung

Try this:

试试这个:

sudo -i -u postgres psql

This accomplishes (almost) the same thing as your

这完成(几乎)与您的任务相同的事情

sudo su - postgres 

The -in the above indicates that you want to use the postgres account's environment. If you remove the -, it will fail similarly to sudo -u

-上述表示要使用Postgres的帐户的环境。如果您删除-,它将失败类似于sudo -u

The -iindicates that you want to run the postgres account's login shell (hence cding to their home directory).

-i表明您要运行的Postgres的帐户的登录shell(因此cd荷兰国际集团到自己的主目录)。

回答by Pipo

For me this did the trick (or you'll get a could not change directory to "/root": Permission denied), pay attention to quotes (')

对我来说,这成功了(否则你会得到一个could not change directory to "/root": Permission denied),注意引号 (')

sudo -Hiu postgres 'pg_dump --column-inserts --data-only --table=someTable entities_db > /var/backups/anywhere/$(date +%Y%m%d_%H%M%S)_someTable.sql'

Note the -Hiufor sudo, or use su - postgres

注意-Hiusudo,或使用su - postgres

you can also put that in a cronjob for root with crontab -e

你也可以把它放在 root 的 cronjob 中 crontab -e