For a one-liner, like migrating a database you can use --dbname
followed by a connection string (including the password) as stated in the pg_dump manual
In essence.
pg_dump --dbname=postgresql://username:[email protected]:5432/mydatabase
Note: Make sure that you use the option --dbname
instead of the shorter -d
and use a valid URI prefix, postgresql://
or postgres://
.
The general URI form is:
postgresql://[user[:password]@][netloc][:port][/dbname][?param1=value1&...]
Best practice in your case (repetitive task in cron) this shouldn't be done because of security issues. If it weren't for .pgpass
file I would save the connection string as an environment variable.
export MYDB=postgresql://username:[email protected]:5432/mydatabase
then have in your crontab
0 3 * * * pg_dump --dbname=$MYDB | gzip > ~/backup/db/$(date +%Y-%m-%d).psql.gz