How to persist and backup data of a PostgreSQL Docker container

Never lose your database information ever again!

Siraphob K.
CodeX
4 min readJan 8, 2022

--

Table of contents

  • Introduction
  • Persisting database information with a mounted volume
  • Dumping and restoring database information

Introduction

Have you ever lost database information using a PostgreSQL docker container? We all know that we should never use a docker container to hold database data because when deleted, the data is gone with it, right? right???

OMG, YOU DID.

And so did I. At the time, I forgot to think about where the container holds its information. I was once using a PostgreSQL container as a database instance for my application. Before deploying for production, I thought I should restart this once to refresh it. (Idk why but yea…) I typed docker-compose down followed with docker-compose up. Then, a panic comes in. I opened my application and found out that all my tables, all my configurations were gone into oblivion. The result was I had to reconfigure everything. Took me quite some time to get it working again. The good thing is at least it didn’t happen during production.

In this article, I will show you how to persist data of a PostgreSQL Docker container and how to restore them.

Persisting PostgreSQL database information with a mounted volume

We’ll first create our database using docker-compose. Please copy the following script into a docker-compose file then run docker-compose up.

Please notice on the volumes block. You can see that we mounted a volume data/ in the host machine to /var/lib/postgresql/data directory of the PostgreSQL container. This is a necessary step to do to persist data on the host machine. Because when the container is deleted, this directory will continue to exist.

After you start the container, you’ll see a data/ directory showing up in your host machine. This directory is where all PostgreSQL information is.

Let’s test that our theory is correct. Shell into the database container by running the following command.

Once you’re inside the container, run the following command to connect to the PostgreSQL console.

Let’s create a table and insert some data.

If you select rows from the table accounts, you’ll see the following result.

After that, exit from the container. And run docker-compose down to stop the container and remove it. Technically, everything should be gone. But as you can see that the data/ directory persists.

Next, run docker-compose up again to start the database container. If you shell into the container and login into the PostgreSQL console. You can see that your table data isn’t lost.

And that’s how you persist database information of a docker container.

Dumping and restoring PostgreSQL database information

Another way to backup database information is to dump it out. A database dump is an export utility that helps you export database meta-data and data rows into a file. The dump file could later be imported into a new database.

Dumping allows you to export only meta-data (schemas, tables, relationships) or both meta-data and data rows. In this case, I’m going to show you how to export all information from a database.

To backup your Postgres database from a docker container, please run the following command.

If you open the dump file, scroll down and you’ll see that it has our database information.

If you want to restore database information from a dump file, please run the following command.

You could try this on your own on a fresh Postgres container to see the effect.

And that’s it for today. Now you’ll never lose your database information again. Happy coding!

--

--