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:
#hostname:port:database:username:password localhost:5432:dbname:dbusername:password
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:
/usr/bin/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.
Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.
We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.