Export PostgreSQL Database from Docker Container

Oct 19, 2020 min read

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. You might have a configuration like this for local development or testing.

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:

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