Easy Database Backups with PostgreSQL: Meet pg_dump and pg_restore!

Pawan DubeyPawan Dubey
2 min read

PostgreSQL, the data wizard of databases, comes with two awesome tools—pg_dump and pg_restore. These tools make backing up and restoring your data a piece of cake. Let's take a simple journey to understand these PostgreSQL gems!

What's the Deal with pg_dump?
Think of pg_dump as a storyteller for your database. It creates a special story (backup) that has all the details about how your database is structured and what's inside it. Why? So you can keep your data safe and use it whenever you need.

Why Do You Need pg_dump?
Backing up your data is like giving it a superhero shield! It protects your database from accidents, making sure it stays strong. Consider pg_dump as your loyal sidekick, always ready to rescue your data.

Demystifying the pg_dump Command:
Before you start, make sure you're in the right place! For Windows computers, go to the bin folder inside the place where PostgreSQL is installed.

cd C:\Program Files\PostgreSQL\Your_PostgreSQL_Version\bin

Now, let's use pg_dump:

pg_dump --dbname=your_database -h your_host -U your_username -p your_port -Ft -f "Your\Path\To\Backup\File.tar"
  • --dbname: Your database's name.

  • -h, -U, -p: Your host, username, and port.

  • -Ft: Save in a special format, in our case its t i.e. tar

  • -f: Choose where to save the backup.

Now, go ahead and fill in your database details. It might ask for a password when you run it.

Meet pg_restore: The Restoration Hero
When things get messy, pg_restore is here to save the day. It rebuilds your database using the special story (backup) made by pg_dump.

Revealing the pg_restore Command:
Again, make sure you're in the bin folder. Now, let's use pg_restore:

pg_restore --clean --if-exists --dbname=your_database -h your_host -U your_username -p your_port -Ft "Your\Path\To\Backup\File.tar"
  • --clean: Clears the slate before restoring.

  • --if-exists: Avoids errors if things don't exist.

  • --dbname: Your target database.

  • -h, -U, -p: Your host, username, and port.

  • -Ft: Confirms the file format.

Now, fill in your database details. It might ask for a password when you run it.

Wrapping Up: PostgreSQL's Backup Dance
Mastering pg_dump and pg_restore is like learning a simple dance—easy moves that make your data safe. Backups become a routine, and restoring is a breeze. Thanks to PostgreSQL's magic, your data is always ready to take center stage! Additionally, when restoring, the process is forceful; if there exists any data with the same table name, it will override it, and if the table does not exist, pg_restore will create them.

1
Subscribe to my newsletter

Read articles from Pawan Dubey directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pawan Dubey
Pawan Dubey

Pawan is working as a DevOps engineer with several years of experience, has good hands on experience on implementing CI/CD and DevOps processes along with Infrastructure automation. He is well versed with the cloud technology including Azure and has sound understanding of application development, deployment, troubleshooting.