What You’ll Learn
In this lesson, you will learn how to create a backup of a PostgreSQL customer database and restore that backup for testing or recovery.
- Understand the difference between
pg_dump,pg_restore, andpsql. - Create a PostgreSQL backup in a format suitable for restoration.
- Restore a customer database into a separate testing database.
- Verify that the restored data is available.
The Concept
A database backup is a saved copy of a database’s structure and data. You can use it to recover from accidental data loss, test an application with realistic data, or migrate data to another PostgreSQL server.
PostgreSQL provides command-line tools for common backup and restore tasks:
pg_dumpcreates a backup of one PostgreSQL database.pg_restorerestores backups created in PostgreSQL’s custom or directory formats.psqlcan restore a plain-text SQL backup by executing the SQL statements in the file.createdbcreates an empty database that can receive the restored objects.
A typical beginner-friendly workflow is:
- Back up the production database with
pg_dump. - Create a separate empty database for testing.
- Restore the backup into the testing database.
- Run a query to confirm that the expected data exists.
The commands below assume that PostgreSQL’s client tools are installed and that your operating system user can connect to PostgreSQL. Avoid placing passwords directly in shell commands. For safer ways to protect database passwords and secrets, use PostgreSQL’s password file or another approved secret-management method.
Basic Example
Suppose a database named customer_db contains customer accounts. The following Bash commands create a compressed custom-format backup, create a separate testing database, and restore the backup into it.
#!/usr/bin/env bash
SOURCE_DATABASE="customer_db"
BACKUP_FILE="customer_db_backup.dump"
TEST_DATABASE="customer_db_testing"
pg_dump \
--format=custom \
--file="$BACKUP_FILE" \
"$SOURCE_DATABASE"
dropdb --if-exists "$TEST_DATABASE"
createdb "$TEST_DATABASE"
pg_restore \
--dbname="$TEST_DATABASE" \
"$BACKUP_FILE"
psql \
--dbname="$TEST_DATABASE" \
--command="SELECT COUNT(*) AS customer_count FROM customers;"Expected Output
The backup and restore commands may produce different messages depending on your PostgreSQL version and settings. The final query should display the number of rows in the restored customers table. For example:
customer_count
----------------
1250
(1 row)How the Code Works
SOURCE_DATABASE stores the name of the database being backed up. BACKUP_FILE is the file that will contain the backup, and TEST_DATABASE is the separate database used for testing.
The pg_dump command creates the backup:
--format=customcreates PostgreSQL’s custom archive format. This format works withpg_restoreand is usually more flexible than a plain SQL file.--file="$BACKUP_FILE"tells PostgreSQL where to save the backup."$SOURCE_DATABASE"identifies the source database.
The dropdb --if-exists command removes an old testing database if one is already present. The --if-exists option prevents an error when the database does not exist. This is convenient for repeatable testing, but never use it against a production database without carefully checking the database name.
createdb "$TEST_DATABASE" creates an empty database. The restored tables and data will be placed there.
pg_restore --dbname="$TEST_DATABASE" "$BACKUP_FILE" reads the custom-format archive and restores it into the testing database. Because the backup was created without a database-creation option, the destination database must exist first.
Finally, psql --command=... runs a SQL query against the restored database. The query counts rows in the customers table, giving you a simple check that the data was restored.
Another Example
Sometimes you need a plain SQL backup instead of a custom-format archive. A plain SQL backup is a text file containing SQL statements such as CREATE TABLE and INSERT. You restore it with psql, not pg_restore.
This example backs up a customer database for migration to another PostgreSQL server. The connection options specify the server address, port, and user explicitly.
SOURCE_DATABASE="customer_db"
BACKUP_FILE="customer_db_migration.sql"
TARGET_DATABASE="customer_db_migrated"
TARGET_HOST="new-db.example.com"
TARGET_PORT="5432"
TARGET_USER="migration_user"
pg_dump \
--format=plain \
--file="$BACKUP_FILE" \
--dbname="$SOURCE_DATABASE"
createdb \
--host="$TARGET_HOST" \
--port="$TARGET_PORT" \
--username="$TARGET_USER" \
"$TARGET_DATABASE"
psql \
--host="$TARGET_HOST" \
--port="$TARGET_PORT" \
--username="$TARGET_USER" \
--dbname="$TARGET_DATABASE" \
--file="$BACKUP_FILE"For a remote PostgreSQL server, authentication may prompt for a password or use a configured password file. The database must also allow the specified user to connect and create or modify the required objects. If PostgreSQL runs on a managed Linux server, you can also run commands on remote Linux instances without opening an SSH session.
Common Mistakes
- Using
pg_restorewith a plain SQL file: A backup made with--format=plainshould be restored withpsql --file=.... Usepg_restorefor custom or directory-format backups. - Restoring over the production database: Always check the value of the destination database before running
dropdbor a restore command. Use a separate testing database when practicing. - Forgetting database permissions: The PostgreSQL user needs permission to connect to the destination database and create the restored objects. Some objects may also require ownership or additional privileges.
- Assuming a successful command proves the backup is usable: A backup file can exist while still being unsuitable for recovery. Restore it into a test database and query important tables regularly.
- Overwriting a backup unintentionally: Choose a clear filename and consider including a date in automated backup filenames, such as
customer_db_2026-08-18.dump.
Try It Yourself
Create a custom-format backup of a database named customer_db and save it as customer_practice.dump. Then create a database named customer_practice_testing, restore the backup into it, and query the number of rows in the customers table.
Before running the commands, confirm that you are connected to the correct PostgreSQL server and that the testing database name is not a production database.
Challenge
You are preparing a customer database for an application test. Write Bash commands that:
- Back up
customer_dbtocustomer_test_backup.dumpusing PostgreSQL’s custom format. - Re-create a separate database named
customer_test_db. - Restore the backup into the testing database.
- Verify the restore by counting rows in the
customerstable.
Solution
SOURCE_DATABASE="customer_db"
BACKUP_FILE="customer_test_backup.dump"
TEST_DATABASE="customer_test_db"
pg_dump \
--format=custom \
--file="$BACKUP_FILE" \
--dbname="$SOURCE_DATABASE"
dropdb --if-exists "$TEST_DATABASE"
createdb "$TEST_DATABASE"
pg_restore \
--dbname="$TEST_DATABASE" \
"$BACKUP_FILE"
psql \
--dbname="$TEST_DATABASE" \
--command="SELECT COUNT(*) AS restored_customers FROM customers;"This solution creates a custom-format backup, ensures that the testing database starts empty, restores the archive into that database, and runs a verification query. The source database is never used as the restore destination.
Key Takeaways
pg_dumpcreates a backup of one PostgreSQL database.- Custom-format backups use
pg_restore, while plain SQL backups usepsql. - Restore backups into a separate testing database before relying on them for recovery.
- Database names, connection settings, permissions, and passwords all affect whether a backup command succeeds.
- A restore test is an important part of proving that a backup can actually be used.



