How to Back Up and Restore PostgreSQL Databases
Regular backups protect your data. PostgreSQL includes pg_dump and pg_restore for this.
1. Back up a database
pg_dump -U myuser -h localhost myapp > myapp_backup.sqlFor a compressed, custom-format backup (recommended for large databases):
pg_dump -U myuser -h localhost -Fc myapp > myapp_backup.dump2. Restore from a plain SQL backup
psql -U myuser -h localhost myapp < myapp_backup.sql3. Restore from a custom-format backup
pg_restore -U myuser -h localhost -d myapp myapp_backup.dump4. Automate with cron
0 2 * * * pg_dump -U myuser myapp > /backups/myapp_$(date +\%F).sqlStore backups off-server (object storage or another host) for disaster recovery.