How to securely recover data from a corrupted PostgreSQL Docker container
December 30, 2025
3 min read
Preview

With the recent incident of Cloudflare, many services world-wide had severe impacts. I've a shared hosting server where I've had deployed my clients' website and also databases are maintained in the same. As cloudflare went down, all of my docker containers collapsed and got corrupt. Because of this, even when I managed to brink back the websites live, they couldn't read data from the database. That day I realized the importance of DB replications (horizontal scaling) and frequent DB backup.

 

Today we are going to look into how we can secure data from the corrupt Postgres container.


ā„¹ļø Note that this only works if the docker volume (where the data are stored) is not deleted.

 

Steps to perform to secure the data from corrupt database container:

  1. Check if the docker volume exists:

    The following command helpts to check the volume.
    docker volume ls

     

  2. Note the volume name you want to secure. If you don't see the volume, then you cannot recover it. This means the data are actually deleted.

  3. Run a new Postgres docker container with the existing volume. Note to use the same docker Postgres version as it was before. For example: I had Postgres version 17 and volume with name `captain--floraldb`
    docker run -d \
      --name postgres17 \
      -e POSTGRES_PASSWORD=mysecret \
      -v captain--floraldb:/var/lib/postgresql/data \
      postgres:17

     

  4. After this, make a dump of the .sql file.
    docker exec -it <container_name_or_id> pg_dumpall -U postgres > dump.sql

     

  5. Start a new Postgres container with a fresh volume and required version. Used Postgres 18 here.
      docker run -d \
      --name postgres18 \
      -e POSTGRES_PASSWORD=<password> \
      -v captain--postgresql-blog-data:/var/lib/postgresql \
      postgres:18

     

  6. Restore the dump
    docker exec -i postgres18 psql -U postgres < dump.sql

     

  7. Check if the data is restored
    1. docker exec -it <postgres-container-id> sh
    2. psql -U postgres
    3. List database: \l
    4. Connect database: \c <db-name>
    5. Query database: run select query to verify your data

     

And that's all. With these steps, you can easily recover the corrupt database. šŸ”„

Ā© 2026 Sandeep Bajracharya