How to Back up PostgreSQL Database
02 Mar, 2026
Introduction
Backing up your PostgreSQL database is a critical task to ensure data integrity, security, and availability. Regular backups protect you from data loss caused by unexpected events such as hardware failures, software issues, or accidental deletions. This process is vital for maintaining the reliability of your database and ensuring you can recover your data in case of any issues. PostgreSQL supports cross-platform capabilities, making it a versatile choice for many users. Its ability to integrate seamlessly with other technologies further enhances its utility.
This article shows you how to back up a PostgreSQL database.
Prerequisites
Before you begin:
- Purchase an Ubuntu 24.04 VPS server. If you don't have a VPS server, sign up with Vultr and get upto $300 worth of free credit to test the Vultr platform.
-
SSH to your VPS server using PuTTY for Windows or run the following command if you're using Linux or Mac.
console$ ssh username@vps_server_public_ip_address -
Create a non-root user with sudo privileges. Read our guide on How to Create a Non-Root Sudo User on Ubuntu 24.04. You'll use this user's account to run the commands in this guide.
- Install a PostgreSQL server. Follow the guides below to install PostgreSQL:
Back up PostgreSQL Database
This section walks you through the steps to back up a PostgreSQL database.
-
Create a directory to store the backup files.
console$ sudo mkdir -p /var/backups/postgresql -
Take ownership of the new directory.
console$ sudo chown -p /var/backups/postgresql -
Use the
pg_dumpcommand to back up the database. Replaceyour_database_namewith the name of your database andbackup_file.sqlwith the desired backup file name. You can append the date and time in the SQL file.console$ sudo pg_dump -d your_database_name -U postgres > /var/backups/postgresql/backup_file.sql
Verify PostgreSQL Backup
You should verify your PostgreSQL backup by listing the files in the backup directory to ensure all necessary files are present.
-
Check the contents of the backups directory to ensure it has been created correctly.
console$ ls -l /var/backups/postgresql/Output:
137479 Jan 2 11:45 backup_file.sql
Conclusion
This guide shows you how to backup a PostgreSQL database. You created a backup directory, performed the backup using pg_dump, and verified the backup file. For further data protection, consider setting up automated backup schedules and storing backups in secure locations. Ensure you periodically test your backups to confirm data reliability.