---
title: Export PostgreSQL Database from Docker Container
description: If you want to export data (data and/or schema) from your Docker container, here's a simple command.
date: 2020-10-19
tags: Docker, Database, Postgres
source: https://brianchildress.co/blog/export-database-from-docker
---

# Export PostgreSQL Database from Docker Container

This simple command can be run from a terminal to export data (and schema) to a local file from the Docker database container. This assumes that data only lives within the container and is not persisted using a [Docker Volume](https://docs.docker.com/storage/volumes/). You might have a configuration like this for local development or testing.

```sh
docker exec -t <Database Server Container Name> pg_dumpall -c -s -U <Database User> > <host/location/and/filename>
```

In practice the command will look something like this:

```sh
docker exec -t my-db pg_dumpall -c -s -U db-user > ./export.sql
```

Breakdown of pg_dumpall flags:

| Flag : | : Meaning |
| :--- | :----- |
| -c   | Include SQL commands to clean (drop) databases before recreating them. DROP commands for roles and tablespaces are added as well. |
| -s   | Dump only the object definitions (schema), not data.                                                                              |
| -U   | Username of database user with export access                                                                                      |

Additional pg_dumpall configuration [options](https://www.postgresql.org/docs/9.2/app-pg-dumpall.html)
