Is there a tool to manipulate pg_hba.conf programmatically (if not what comes closest)?

1

I'm looking for anything above the level of programmatic file manipulation, a command or a python library. There seems to be no alternative to editing the file (assuming that http://www.postgresql.org/docs/9.2/static/auth-pg-hba-conf.html would mention an alternative).

Karl Richter

Posted 2014-06-20T23:08:15.673

Reputation: 1 641

Question was closed 2014-06-21T19:07:38.003

I think this should be asked on the Software Recommendations SE.

– Cristian Ciupitu – 2014-06-21T00:53:30.317

@@Karl Richter, if you're happy with my answer, you can still accept it even if the question is closed. – Cristian Ciupitu – 2014-06-23T08:41:39.510

Answers

6

Try augeas. It offers both Python bindings and a command-line interface - augtool.

[dba@pgsql ~]$ augtool
augtool> defvar pghba /files/var/lib/pgsql/data/pg_hba.conf
augtool> ####################################
augtool> ls $pghba
#comment[1] = this is a comment
1/ = (none)
2/ = (none)
#comment[2] = this another comment
3/ = (none)
augtool> ####################################
augtool> ls $pghba/1
type = local
database = all
user = all
method = trust
augtool> ####################################
augtool> insert 01 after $pghba/2
augtool> set $pghba/01/type host
augtool> set $pghba/01/database db1
augtool> set $pghba/01/user user1
augtool> set $pghba/01/address 127.0.0.1
augtool> set $pghba/01/method md5
augtool> ####################################
augtool> save
Saved 1 file(s)

(Comments start with #)

Cristian Ciupitu

Posted 2014-06-20T23:08:15.673

Reputation: 4 515

Thanks for this. First, this use-case is augtool -i. Second, my pg_hba.conf is not locaed in that directory -- how do I get augtool to recognize/use the pg_hba lens for the arbitrary location? Third, I've seen the augeas documentation page for the pg_hba lens and .. Greek is easier reading: How do you figure out how to "translate" the lens documentation into commands you've used above? – Otheus – 2015-08-23T12:02:26.147

I found the answers to the question marked as "Second". In augtool, I can simply use: set /augeas/load/Pg_Hba/incl[last()+1] "/etc/postgresql_hba.conf" followed by load. – Otheus – 2015-08-23T12:51:37.630

@Otheus, I looked at the examples and changed them a bit. – Cristian Ciupitu – 2015-08-24T21:54:28.493