Jacob Lyles
Jacob Lyles

Reputation: 10390

Unix script can't alter postgres hba.conf configuration file on Ubuntu

I'm attempting to setup postgres 9.6 on ubuntu/vagrant through a provisioning script. Part of my script adds a line to pg_hba.conf with the following command:

sudo -u postgres echo "host all all all md5" >> /etc/postgresql/9.6/main/pg_hba.conf

However, this gives me the error -bash: /etc/postgresql/9.6/main/pg_hba.conf: Permission denied

Which is strange because I am allowed to edit the file with either sudo nano or sudo -u postgres nano.

Here are the permissions on the file: -rw-r----- 1 postgres postgres 4641 Apr 6 16:11 pg_hba.conf

How can I add this line to my configuration file in a script?

Upvotes: 1

Views: 733

Answers (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.


How bash executes commands

Redirections

Upvotes: 1

Related Questions