Announcing 🎉 Flat-Rate pricing 🎉 to keep your costs predictable and under control See More

Use Docker to Backup and Restore PostgreSQL Databases

Joe Scharf

Joe Scharf

Updated

4 min read

Computer with a hard drive sitting next to it.

Overview

Docker is a convenient way to gain access to applications and their utilities without having to install them on your local machine. By using Docker, you can quickly pull an image of an application and run the necessary commands within a container. When the commands are done running, everything is conveniently cleaned up, eliminating the clutter associated with a local application install.

This guide will walk you through the steps to use Docker to backup a PostgreSQL database. This approach works with databases running in a Docker container, locally hosted, or on a remote server.

This is the approach DBSnapper uses to backup and sanitize PostgreSQL database when using the pgdocker database engine. DBSnapper takes care of the details for you, so you don't have to worry about it.

The steps we will follow:

  1. Pull the PostgreSQL Docker image
  2. Backup with the pg_dump command
  3. Restore with the pg_restore command

Prerequisites

  • Docker installed on your local machine

Pull the PostgreSQL Docker image

The first step is to docker pull the PostgreSQL Docker image. This will download the image from Docker Hub and make it available to run as a container on your local machine.

docker pull postgres:latest

This command pulles the latest version of the PostgreSQL image from Docker Hub. You can also specify a specific version of the image by appending the version number to the image name. For example, to pull version 15.5 of the PostgreSQL image, you would run the following command:

docker pull postgres:15.5

Backup with the pg_dump command

Once the image is downloaded, you can run the pg_dump command in the docker container to backup the database. The command below will backup the mydb database to a file named mydb.sql in the current directory. The --rm flag tells Docker to remove the container when the command is finished running.

docker run --rm -v $(pwd):/backup postgres:latest pg_dump -h <host> -U <user> -d <database> -f /backup/mydb.sql

The command above mounts the \backup directory in the container as a volume -v to the host's current directory $(pwd). This allows the pg_dump command to write the backup file to the current directory on your local machine.

See our article: How to Create a PostgreSQL Database Snapshot to learn more about the PostgreSQL pg_dump utility.

Restoring with the pg_restore command

If you need to restore the database from the backup file, you can use the pg_restore command in the docker container. The command below will restore the mydb database from the mydb.sql file in the current directory.

docker run --rm -v $(pwd):/backup postgres:latest pg_restore -h <host> -U <user> -d <database> -f /backup/mydb.sql

See our article: How to Restore a PostgreSQL database snapshot with pg_restore to learn more about the PostgreSQL pg_restore utility.

Conclusion

Using Docker to backup PostgreSQL databases is a convenient way to backup your database without having to install PostgreSQL on your local machine. This approach also works with databases running in a Docker container, locally hosted, or on a remote server.

DBSnapper simplifies database snapshot management

The DBSnapper app has the ability to use Docker images to access the PostgreSQL database utilities and simplifies creating, sanitizing, and restoring database snapshots. When combined with the DBSnapper Cloud, management of your snapshots is much easier with private cloud storage profiles and team sharing options. Install the DBSnapper Agent to get started, and Sign up for the DBSnapper Cloud today!

If you have any questions or feedback on this article, please get in touch on our contact page.


References