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
the easiest way in my opinion, this: you edit you main postgres config file: pg_hba.conf there you have to add the following line:
host <you_db_name> <you_db_owner> 127.0.0.1/32 trust
and after this you need start you cron thus:
pg_dump -h 127.0.0.1 -U <you_db_user> <you_db_name> | gzip > /backup/db/$(date +%Y-%m-%d).psql.gz
and it worked without password
If you want to do it in one command:
PGPASSWORD="mypass" pg_dump mydb > mydb.dump
Another (probably not secure) way to pass password is using input redirection i.e. calling
pg_dump [params] < [path to file containing password]
Create a .pgpass
file in the home directory of the account that pg_dump
will run as.
The format is:
hostname:port:database:username:password
Then, set the file's mode to 0600
. Otherwise, it will be ignored.
chmod 600 ~/.pgpass
See the Postgresql documentation libpq-pgpass for more details.