views:

224

answers:

2

I'm trying to create a cronjob to back up my database every night before something catastrophic happens. It looks like this command should meet my needs:

0 3 * * * pg_dump dbname | gzip > ~/backup/db/$(date +%Y-%m-%d).psql.gz

Except after running that, it expects me to type in a password. I can't do that if I run it from cron. How can I pass one in automatically?

+3  A: 

Create a .pgpass file in the home directory of the account that pg_dump will run as: see http://www.postgresql.org/docs/8.4/static/libpq-pgpass.html for details of the format (including the last para where it explains it will be ignored if you don't set the mode to 0600).

araqnid
Yep. That worked.
Mark
+1  A: 

You should also consider using pg_dump -Fc instead of that gzip pipe sillyness.

Joshua D. Drake
I got that from the official docs! http://www.postgresql.org/docs/8.1/interactive/backup.html#BACKUP-DUMP-LARGE
Mark
Yeah I know, the official docs are kind of borked in terms of the backup chapter. We are hoping to get them cleaned up for the upcoming 9.0 or 9.1 release. Anyway you really should be using pg_dump -Fc.
Joshua D. Drake