Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass in password to pg_dump?

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?

like image 973
mpen Avatar asked May 23 '10 23:05

mpen


People also ask

What is path to pg_dump?

c:\Program files\postgresql\9.3\bin> pg_dump -h localhost -p 5432 -U postgres test > D:\backup. sql ... After above command enter User "postgres" password and check D:\ drive for backup.sql file. Follow this answer to receive notifications.

What is the format for pg_dump?

pg_dump can be used to backup an entire database, then pg_restore can be used to examine the archive and/or select which parts of the database are to be restored. The most flexible output file formats are the “custom” format ( -Fc ) and the “directory” format ( -Fd ).

Does pg_dump lock?

pg_dump doesn't lock the entire database, it does get an explicit lock on all the tables it is going to dump, though.

What does the pg_dump command do?

The pg_dump command extracts a PostgreSQL database into a script file or another archive file. This utility is for backing up databases. The utility makes consistent backups even if the database is being used concurrently. Readers, writers, and other users won't be blocked from using the database while using pg_dump .


1 Answers

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.

like image 133
araqnid Avatar answered Sep 20 '22 09:09

araqnid