我试图通过配置脚本在ubuntu / vagrant上设置postgres 9.6。我的部分脚本使用以下命令向pg_hba.conf
添加一行:
sudo -u postgres echo "host all all all md5" >> /etc/postgresql/9.6/main/pg_hba.conf
然而,这给了我错误-bash: /etc/postgresql/9.6/main/pg_hba.conf: Permission denied
这很奇怪,因为我可以使用sudo nano
或sudo -u postgres nano
编辑文件。
以下是文件的权限:
-rw-r----- 1 postgres postgres 4641 Apr 6 16:11 pg_hba.conf
如何在脚本中将此行添加到配置文件中?
答案 0 :(得分:1)
The problem here is that redirection happens before command execution. So the redirection doesn't have the elevated privileges you expected it to.
There's more than one way around that problem. I generally use something like this.
echo "host..." | sudo tee -a /etc/postgresql/9.6/main/pg_hba.conf
Piping to sudo tee...
avoids problems with quoting.