This article describes how to set up a cron job that automatically backs up a PostgreSQL database to a file at specific intervals.
A cron job enables you to automatically back up a PostgreSQL database at an interval you define. A cron job runs without any user interaction, though, so you must do some extra configuration steps that enable cron to access a database without being prompted to enter a password.
To do this, follow these steps:
At the command prompt, type the following command:
chmod 600 .pgpass
Create a cron job that runs the following command. Replace dbusername with the user who has access to the database, replace dbname with the name of the database you want to back up, and replace path with the path where you want to store the database backup file. This example uses backup.pgsql for the backup's filename, but you can use any filename you want:
pg_dump --no-password -U dbusername dbname > /path/backup.pgsql
Subscribe to receive weekly cutting edge tips, strategies, and news you need to grow your web business.
No charge. Unsubscribe anytime.