This website use cookies to help you have a superior and more admissible browsing experience on the website.
Loading...
PostgreSQL is popular open-source object-relational database management system (RDBMS). For any business driven by PostgreSQL, the database can be the most critical asset.
Whether you are a developer managing a local project or a DBA overseeing a production cluster, a reliable strategy to backup PostgreSQL database important for protecting data from human error, system failure, ransomware attack and ensuring compliance.
In this guide, we will walk you through the most reliable methods to backup and restore your PostgreSQL database, from the command line and pgAdmin to modern Docker environments and automation.
The command-line interface (CLI) is the common way to manage PostgreSQL backups.It uses two native utilities: pg_dump for creating the backup and psql or pg_restore for bringing the data back. This method support backup specific tables, and placed into scripts for scheduling. but it doesn’t support point-in-time recovery.
Here are detailed steps:
Step 1. Navigate to PostgreSQL bin folder. Usually it locates: C:\Program Files\PostgreSQL\16\.
Step 2. Find the “bin” folder and right-click on it, and choose “Open in Terminal“.
Step 3. To create a standard backup of a single database, use the following structure:
pg_dump -U [username] -W -F t [database] > c:\[backup location]
Step 4. You will be asked the password. Type your password and press “Enter“. When the backup is finished, go to the backup location and check if the backup files.
Note:
1. If you want to backup all PostgreSQL, please use the ./pg_dumall. If you want to only backup schema, please use ./pg_dumpall.
2. If you are using Docker, to backup PostgreSQL databases in Docker:
3. If you want to backup PostgreSQL to AWS S3, please configure the AWS CLI:
To restore postgreSQL, you can use the following scripts.
1. Restoring a Plain Text (.sql) File:
psql -U [username] -d [target_database] < [backup_file].sql
2. Restoring a Custom/Compressed (.dump) File:
Required if you used the -Fc flag.
pg_restore -U [username] -d [target_database] [backup_file].dump
pgAdmin 4 provides a powerful graphical way to manage backups. This method is ideal for one-off backups, beginners, or developers who want to visually select specific database objects. But pgAdmin is harder to automate; slower for very large databases
Follow the steps below:
Step 1. Open pgAdmin, expand the “Servers” tree, and find the database you wish to backup.
Step 2. Right-click the database name and select “Backup…” from the context menu.
Step 3. Then configure the setting:
Step 4. In the Options tab, you can choose to “Only Data” or “Only Schema” if you don’t need a full backup.
Step 5. Click Backup to execute. You can monitor the progress in the “Processes” dashboard at the bottom right.
To restore database from pgadmin4, follow the steps below:
Step 1. Create a fresh, empty target database
Step 2. Right-click the target database and select “Restore…“.
Step 3. Select the File:
Step 4. Under the Options tab, enabling “Clean before restore” is useful if you want to drop existing objects before recreating them.
Step 5. Click “Restore”. A notification will appear once the process is complete.
DBeaver is a favorite among developers because it is a universal tool that works with almost any database engine. While it provides a slick interface for “Quick Dumps,” it relies on your local PostgreSQL installation to do the heavy lifting.
DBeaver comes with intuitive UI and useful for quick data export, but it is not the best option for scheduling protection and is heavywright since it is Java-based.
Then below steps are how to use DBeaver.
After the prepration, following the steps below.
Step 1. Right-click on your database name, navigate to “Tools“, and select “Backup“.
Step 2. Choose the specific schemas or tables you want to include.
Step 3. Choose a formation – Plain or Custom
Step 4. Choose your destination (local disk or external drive) and click “Start“.
Manual backups are fine for one-off tasks, but production databases require automation. On Linux and macOS, the most reliable way to schedule backups is using Cron Jobs. But if you are not familier with Cron Jobs, it may different to operate for you.
Cron jobs run in the background, so they cannot “type” a password when prompted. You have two professional options to handle this:
Instead of putting a long command directly in the crontab, it is better to create a simple bash script (backup_db.sh). This allows you to add features like date-stamping and automatic cleanup.
#!/bin/bash
# Configuration
DB_NAME=”my_database”
BACKUP_DIR=”/home/user/backups”
DATE=$(date +%Y-%m-%d_%H-%M)
# Create the backup (compressed)
export PGPASSWORD=’your_secure_password’
pg_dump -U postgres -d $DB_NAME | gzip > $BACKUP_DIR/db_backup_$DATE.sql.gz
# Optional: Delete backups older than 30 days to save space
find $BACKUP_DIR -type f -name “*.sql.gz” -mtime +30 -delete
The Crontab is the schedule where you tell the system when to run your script.
Step 1. Open the editor: crontab -e
Step 2. Add a line at the bottom to schedule the backup. For example, to run every night at 2:00 AM:
00 02 * * * /home/user/scripts/backup_db.sh >> /home/user/logs/backup.log 2>&1
Breakdown of the Cron Schedule:
While manual scripts and open-source GUIs are effective for smaller projects, enterprise environments with massive datasets and strict uptime requirements often need a more robust, “hands-off” solution.
For organizations that cannot afford data loss or manual configuration errors, i2Backup from Info2soft provides a professional and easy way to create scheduled backups for all critical PostgreSQL databases. It support multiple backup storage, like local/external disk, ZFS, NAS, deduplicate stoage, AWS S3 storage, and so on.
Key Features and advantages of i2Backup for PostgreSQL:
Comparison: i2Backup vs. Manual Methods
| Feature | Manual Scripts (Cron/CLI) | i2Backup (Enterprise) |
|---|---|---|
| Setup | High manual effort per server | Fast, policy-based deployment |
| Data Transfer | Usually full dumps only | True Incremental support |
| Monitoring | Manual log checking | Automated alerts and reporting |
| Scalability | Hard to manage at scale | Scalable backup node for growth |
Selecting the right method to backup PostgreSQL databases depends entirely on your environment and the value of your data. For developers and small projects, native command-line tools and GUIs like pgAdmin offer a reliable, free way to manage manual snapshots and simple automation. These methods provide the flexibility needed for local development and minor production tasks without additional costs.
However, as data scales, manual scripts often become a liability due to “invisible” failures and high storage demands. For mission-critical environments, an enterprise solution like Information2’s i2Backup is the most efficient choice, offering automated, incremental, and scalable protection that manual methods can’t match. Besides backup PostgreSQL, it also support backup many other platforms, like SQL Server data backup.