Skip to main content
PostgreSQL
CHAPTER 24 Intermediate

Importing, Exporting, Backup, and Recovery

Updated: May 16, 2026
7 min read

# CHAPTER 24

Importing, Exporting, Backup, and Recovery

1. Introduction

All the advanced queries in the world mean nothing if your server's hard drive crashes and you lose the data. Data loss is a catastrophic event that destroys companies. As a database developer, you must know how to move data in and out of your system seamlessly, and more importantly, how to execute bulletproof Backup and Recovery protocols. In this chapter, we will step out of the SQL editor and into the Terminal to master the essential operational tools of PostgreSQL.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Use the COPY command to import and export CSV files.
  • Understand the difference between logical and physical backups.
  • Use pgdump to create a complete database backup.
  • Use pgrestore to rebuild a database from a backup file.
  • Understand the 3-2-1 Backup Strategy.

3. Importing and Exporting CSV Data

Often, a client will hand you an Excel spreadsheet with 10,000 rows and say, "Put this in the database." You do not write 10,000 INSERT statements. You save the spreadsheet as a .csv file and use the incredibly fast COPY command.

Exporting a Table to CSV: Open psql (or the pgAdmin Query Tool) and run:

sql
1
COPY users TO '/var/lib/postgresql/users_backup.csv' DELIMITER ',' CSV HEADER;

*(This takes the entire users table and writes it to a file. The HEADER keyword ensures the column names are printed on the first line).*

Importing a CSV into a Table: *Note: The table must already be created with the correct columns!*

sql
1
COPY users FROM '/var/lib/postgresql/new_users.csv' DELIMITER ',' CSV HEADER;

4. Logical Backups: The pgdump Utility

The COPY command only moves raw data for a single table. It does not copy your Table Structures, Indexes, or Views. To back up an entire database (Data + Architecture), PostgreSQL provides a terminal utility called pgdump. You run this from your operating system's terminal (Bash/PowerShell), NOT from inside the SQL editor!

Creating a plain-text SQL backup:

bash
1
pg_dump -U postgres -d university_db > backup_file.sql

*(This generates a massive text file containing every CREATE TABLE and INSERT statement needed to magically recreate your database from scratch).*

While .sql text files are great for reading, they are slow to restore. Professional DBAs use the "Custom Format" (-Fc). It is heavily compressed and allows you to restore individual tables later if needed.
bash
12
# -Fc means Format Custom. -f specifies the output file.
pg_dump -U postgres -Fc -d university_db -f university_backup.dump

6. Disaster Recovery: The pgrestore Utility

Your server crashed. You bought a new server, installed a fresh copy of PostgreSQL, and transferred your universitybackup.dump file to it. How do you revive the database?

First, you must create a blank database container:

bash
1
createdb -U postgres university_db_new

Next, use the pg_restore terminal utility to unpack the backup into the empty container:

bash
1
pg_restore -U postgres -d university_db_new -1 university_backup.dump

*(The -1 flag is brilliant: It wraps the entire restoration in a single Transaction. If the restore fails halfway through, it rolls back, preventing a corrupted partial restore!)*

7. Physical Backups and WAL (Advanced)

pgdump is a "Logical" backup. It extracts the logic (SQL commands). For massive enterprise databases (Terabytes of data), logical backups are too slow. Instead, DBAs use "Physical Backups" (copying the actual binary .dat files off the hard drive) combined with WAL (Write-Ahead Logging). WAL files record every single byte change in real-time. By continuously archiving WAL files to an AWS S3 bucket, enterprises can perform "Point-In-Time Recovery," rewinding the database to any exact millisecond before a crash occurred.

8. Mini Project: Automating Backups with Cron

A backup is useless if you forget to run it. In Linux, we use cron to schedule automated tasks. Let's create a bash script (backup.sh) that runs pg
dump and stamps the file with today's date.
bash
123456789
#!/bin/bash
# A simple automated backup script
DATE=$(date +"%Y-%m-%d")
FILE_NAME="/backups/db_backup_$DATE.dump"

pg_dump -U postgres -Fc -d production_db -f $FILE_NAME

# Keep only the last 7 days of backups to save disk space
find /backups/ -type f -name "*.dump" -mtime +7 -delete

*(You would then configure the Linux Cron daemon to execute this script every night at 3:00 AM).*

9. Common Mistakes

  • Never Testing the Recovery: Generating backup files every night is useless if the files are corrupted. A backup does not officially exist until you have successfully executed pgrestore on a test server and verified the data is intact.
  • Storing Backups on the Same Server: If you save your pgdump file to the same hard drive that the database lives on, and that hard drive catches fire, you lose both the database and the backup. Always transfer backups off-site immediately.

10. Best Practices

  • The 3-2-1 Backup Strategy:
  • Keep 3 copies of your data (1 Primary, 2 Backups).
  • Store them on 2 different types of media (e.g., Local Drive + Cloud Storage).
  • Keep 1 copy completely off-site (e.g., in a different physical city or AWS region).

11. Exercises

  1. 1. What terminal utility is used to generate a complete logical backup of a PostgreSQL database?
  1. 2. Why is the -1 flag highly recommended when executing pg_restore?

12. SQL Challenges

Write the SQL command (executed inside psql) to export the entire products table into a CSV file located at C:\backups\products.csv, ensuring the column headers are included.
sql
1
COPY products TO 'C:\backups\products.csv' DELIMITER ',' CSV HEADER;

13. MCQ Quiz with Answers

Question 1

When running the COPY command to import a massive CSV file into PostgreSQL, what must be true about the target table?

Question 2

What is the primary difference between a Logical Backup (pgdump) and a Physical Backup?

14. Interview Questions

  • Q: Describe the architectural differences between pgdump and the COPY command. When would you use one over the other?
  • Q: Explain the concept of Write-Ahead Logging (WAL) in PostgreSQL. How does it facilitate Point-In-Time Recovery in enterprise disaster scenarios?

15. FAQs

Q: Does running pgdump lock the database and prevent users from reading the website? A: No! Because of PostgreSQL's brilliant MVCC architecture, pgdump takes a seamless snapshot. Your website will remain 100% online and functional while the massive backup is running in the background.

16. Summary

You are now equipped to protect your organization's most valuable asset. By utilizing COPY for rapid data onboarding, and deploying automated pgdump and pgrestore workflows, you ensure that your data is highly portable, archivable, and perfectly insulated against catastrophic hardware failures.

17. Next Chapter Recommendation

Our data is backed up against hardware failures, but what about malicious human failures? If a hacker discovers the database password, can they delete everything? In Chapter 25: PostgreSQL Security Best Practices, we will master Roles, Permissions, and the Principle of Least Privilege.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·