1

I want to create a backup of the original pg_hba.conf file (may be as pg_hba.conf.bk) and then add an entry into the existing pg_hba.conf file. So far this is what i've been trying to do, but every time it says permission denied even if i go via root user or use sudo.

My attempt:

$ pg_conftool -s 10 main show hba_file
[OUT] /etc/postgresql/10/main/pg_hba.conf

$ sudo cat /etc/postgresql/10/main/pg_hba.conf
    [OUT] local   all             postgres                              peer
          local   all             all                                   md5
          host    all             all             127.0.0.1/32          md5
          host    all             all             ::1/128               md5

now when i try adding a line into it, i get the permission denied error

$ sudo echo "host all all 0.0.0.0/0 trust" >> /etc/postgresql/10/main/pg_hba.conf
[OUT] bash: /etc/postgresql/10/main/pg_hba.conf: Permission denied

This will be a part of deployment script therefore, I can't really go and make the entry into the pg_hba.conf manually. So, how can i make an entry into the conf file?

Aman Singh
  • 1,111
  • 3
  • 17
  • 31

1 Answers1

2

The way you wrote your command, the redirection >> will be outside the sudo command, so it is executed with the permissions of the normal user. Hence the error message.

Try this command line:

sudo sh -c 'echo "host all all 0.0.0.0/0 trust" >> /etc/postgresql/10/main/pg_hba.conf'
Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
  • you mean like this `sudo 'echo "host all all 0.0.0.0/0 trust" >>/etc/postgresql/10/main/pg_hba.conf'` ?? because this didn't work. could you please exemplify the same.. – Aman Singh Apr 24 '18 at 06:34