How can I activate md5 and peer for the postgres user in pg_hba.conf

Viewed 18

I want to log into my database with

psql -U postgres

from my user 'pi' for example.

To achive this I edited my pg_hba.conf to this (method from peer to md5): enter image description here

My problem is that my cronjob,

* * * * * psql -d db_name -c "insert into timestamps values (now());"

by the user postgres doesn't work anymore, because I now need to log in with the password of every account. Postgres rightly warns, that this will happen. Can I activate both methods (md5 and peer), to log into postgres from every user and have my cronjobrunning?

1 Answers

Not directly. You could have it use peer for local connections and md5 for host connections. Then you would need to specify -h localhost for your non-cronjob. But you could set that in PGHOST for everyone (other than cron) instead of specifying it each time

Another solution would be to use a pg_ident map with peer which would authorize your daily user account to log in as postgres. Of course you would also need to authorize postgres OS user to log in as postgres db user (or change the cronjob to run as your daily user account) because once you start using a map it disables the dummy self-mapping.

Or, you could give cronjob a .pgpass file to use, and just let it use md5 same as everyone else.

Related