Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
VGSources
Blog

How to Back Up a PostgreSQL Database Running in Docker

Run pg_dump inside the PostgreSQL container, redirect the dump to the host, export cluster globals separately, and restore custom archives with pg_restore or SQL files with psql.
WorldDocker Length8 min Posted Quest giverVGSources Team
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The dependable Docker method is to run pg_dump inside the running PostgreSQL container and redirect its output to a file on the Docker host. Use a custom archive (-Fc) when you want selective restores with pg_restore; use plain SQL when you want a human-readable script restored with psql. Back up cluster-wide roles and tablespaces separately with pg_dumpall --globals-only.

What the backup commands actually do

Docker does not change PostgreSQL’s backup formats. It changes where you invoke the PostgreSQL client utility. docker exec starts pg_dump in the already-running database container, where the PostgreSQL client version normally matches the server version. The command’s standard output is redirected by your host shell into a file on the host, so the resulting dump remains available even if the container is later recreated.

As an Amazon Associate I earn from qualifying purchases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A database dump is a logical backup: it contains SQL objects and data that PostgreSQL can load into a database. It is not the same thing as copying the PostgreSQL data directory. The commands below cover logical dumps; a data-volume copy has different consistency considerations and is outside this procedure.

Choose the scope and format first

Need Command family Output Restore with Important limitation
One database, flexible restore pg_dump with -Fc Custom archive pg_restore Not readable as a normal SQL script
One database, readable script pg_dump without -Fc Plain SQL psql Less convenient for selective object restoration
Every database in a cluster pg_dumpall SQL script psql Cluster-wide output is not a pg_restore archive
Roles and tablespaces pg_dumpall --globals-only SQL script containing global objects psql These objects are not included in an individual pg_dump

For most application databases, a custom archive is the practical default because it supports selective restoration and the pg_restore workflow. Choose plain SQL when inspecting or editing the script is more important than archive features.

Prepare the container and destination

Find the running container

Replace the example name postgres or postgres-container with the actual container name or ID. The name must identify the running PostgreSQL container, not merely an image name. If the container is stopped, docker exec cannot run the dump.

Confirm the database connection details

You need the PostgreSQL role, database name, and, when required by the image configuration, authentication details. The examples use either myuser/mydb or postgres/mydatabase. Substitute your real values rather than copying them literally.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The official PostgreSQL image commonly permits local-socket connections from inside the container without a password, but that is an observation about a common configuration, not a guarantee. Custom images, changed pg_hba.conf rules, TCP connections, or password authentication can require credentials.

Pick a host path with enough space

The redirection target, such as mydb.dump or backup.sql, is created on the host in the directory from which you run the command unless you provide an absolute path. Ensure that directory is writable and has room for the dump. A successful command should leave a file at exactly the path you selected.

Back up one database in custom format

Run this from the Docker host:

docker exec postgres pg_dump -U myuser -d mydb -Fc > mydb.dump
  1. Container: postgres is the running container name.
  2. Role: -U myuser selects the PostgreSQL role used to read the database.
  3. Database: -d mydb selects the database being dumped.
  4. Format: -Fc requests PostgreSQL’s custom archive format.
  5. Host file: > mydb.dump sends standard output to a host file instead of printing the archive in the terminal.

Do not add -t or -it to a command whose output is being redirected. A TTY can alter line endings and can mix error output into the dump stream, producing a file that is unsuitable for restoration. If the command reports an error, treat the backup as unsuccessful and correct the connection, permissions, or path before relying on the file.

A custom archive is not intended to be fed directly to psql. PostgreSQL’s documentation describes it as a dump that must be restored with pg_restore.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Back up one database as plain SQL

For a readable SQL script, use:

docker exec -t postgres-container pg_dump -U postgres mydatabase > backup.sql

The positional database name in this form is equivalent to specifying it with -d. The resulting backup.sql is a text script that can be inspected and restored through psql.

Although some examples show -t, avoid allocating a TTY when redirecting output. Use the safer form below:

docker exec postgres-container pg_dump -U postgres mydatabase > backup.sql

Use the plain format when a text file is useful for review or controlled editing. It does not provide the custom archive’s selective restoration behavior; restoration follows the SQL script in order through psql.

Back up every database in the cluster

pg_dump handles one database per invocation. To produce one SQL script containing all databases in the PostgreSQL cluster, run pg_dumpall inside the container:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
docker exec postgres pg_dumpall -U postgres > cluster-backup.sql

As with pg_dump, omit interactive or TTY flags when redirecting. The output from pg_dumpall is a SQL script, so restore it with psql, not pg_restore.

A whole-cluster dump addresses the case where several databases share one PostgreSQL instance. It does not turn the result into a custom archive, and it should not be confused with a single-database pg_dump -Fc file.

Back up roles, passwords and tablespaces separately

An individual pg_dump does not export cluster-level roles, role passwords, or tablespaces. Capture those global objects with:

docker exec postgres pg_dumpall -U postgres --globals-only > globals.sql

The globals file is a SQL script. Restore it through psql in an environment where the required administrative privileges and referenced tablespaces are available. Keep the globals dump with the database dump; restoring application data without its roles or tablespace definitions can leave ownership and placement incomplete.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Restore a custom-format dump

First create or select the target database, then run pg_restore against the archive. A direct restore looks like this from the host:

docker exec -i postgres pg_restore -d mydb mydb.dump

That command assumes the archive is accessible inside the container at mydb.dump. A host file is not automatically visible at the same path inside a container. You can either copy the archive into the container first, or stream it through standard input:

docker cp mydb.dump postgres:/tmp/mydb.dump
docker exec -i postgres pg_restore -d mydb /tmp/mydb.dump

The -i flag is useful when standard input is involved; do not allocate a TTY for binary archive data. For a replacement restore, guides commonly suggest:

docker exec -i postgres pg_restore --verbose --clean --no-acl --no-owner -d mydb /tmp/mydb.dump
  • --verbose prints detailed progress.
  • --clean removes database objects before recreating them. Use it only when that destructive behavior is intended.
  • --no-acl skips restoring access-control changes.
  • --no-owner avoids applying original ownership, which is useful when target roles differ.

Those options change the result; they are not universally correct defaults. On a production database, confirm the target, ownership policy, permissions, and whether existing objects may be dropped before adding them.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Restore a plain SQL or pg_dumpall file

Plain SQL is restored by piping the host file into psql running in the container:

cat backup.sql | docker exec -i postgres psql -U postgres -d mydatabase

For a cluster script produced by pg_dumpall, target the PostgreSQL server’s administrative connection rather than treating the file as a custom archive:

cat cluster-backup.sql | docker exec -i postgres psql -U postgres

Restore the globals script the same way, with the administrative role required by the statements it contains:

cat globals.sql | docker exec -i postgres psql -U postgres

Do not pass a custom-format file to psql; binary archive data belongs to pg_restore. Conversely, pg_restore cannot read the SQL script emitted by pg_dumpall.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use the right PostgreSQL major version

The pg_dump utility should be the same major version as the server or newer. Running it inside the database container is a straightforward way to meet that requirement because the container’s client normally corresponds to its PostgreSQL server.

Newer pg_dump releases can dump servers older than themselves; the PostgreSQL 18 documentation states support for servers back to 9.2. That does not mean every dump can be loaded into every older server. Dump output is not guaranteed to load into an older major version, even when the source and target are otherwise similar, and manual edits may be required.

For a cross-version dump, PostgreSQL’s guidance recommends --quote-all-identifiers:

docker exec postgres pg_dump -U myuser -d mydb -Fc --quote-all-identifiers > mydb.dump

Test a cross-version restore before treating it as a recovery plan. The exact requirement for whether pg_restore must match or exceed the archive’s pg_dump version is not established by the cited guidance, so use a compatible PostgreSQL toolchain and validate the result on the intended target version.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Docker Compose and TTY behavior

With Docker Compose, the equivalent execution command is usually docker compose exec followed by the service name. Compose allocates a TTY by default, so disable it with -T when redirecting a dump:

docker compose exec -T db pg_dump -U myuser -d mydb -Fc > mydb.dump

Here db is an example Compose service name. Replace it with the service that runs PostgreSQL. The key rule is the same as with docker exec: no terminal allocation for redirected dump output.

Common failures and their fixes

Symptom Likely cause Correction
No such container or an execution error The name is wrong or the container is not running Use the current running container name or ID and start the database container before running docker exec.
Password or authentication failure The image does not trust the local socket, or authentication rules require a password Use the configured role and authentication method; do not assume the common official-image behavior applies to every setup.
The dump file is empty or unusable A command failed, a TTY was allocated, or terminal output was mixed into the stream Remove -t/-it, rerun the command, and inspect the command’s exit status and resulting file.
psql reports invalid archive data A custom-format file was sent to psql Restore the -Fc archive with pg_restore.
pg_restore cannot read the input The input is a plain SQL or pg_dumpall script Pipe the script to psql instead.
Objects are owned by missing roles after restore Only the database was dumped Restore the separately created pg_dumpall --globals-only file, or deliberately use --no-owner and assign ownership afterward.
Cross-version restore errors The target major version is older or incompatible Prefer a same-or-newer dump utility, use --quote-all-identifiers for cross-version dumps, and test the restore on the target version.

A practical backup sequence

  1. Identify the running PostgreSQL container or Compose service and the database, role, and host destination.
  2. Choose custom format for selective restoration, plain SQL for a readable single-database script, or pg_dumpall for all databases.
  3. Run the dump inside the container with no TTY while redirecting output to the host.
  4. Run a separate pg_dumpall --globals-only command for roles, passwords, and tablespaces.
  5. Keep the database dump and globals file together and record which PostgreSQL major version produced them.
  6. When recovery is needed, use pg_restore for custom archives and psql for SQL scripts.
  7. For cross-version recovery, validate the complete restore on the intended target before depending on it.

Which command should you use?

  • One database and the most restoration control: docker exec postgres pg_dump -U myuser -d mydb -Fc > mydb.dump, followed by pg_restore.
  • One database as editable text: run pg_dump without -Fc and restore with psql.
  • Several databases in one cluster: run pg_dumpall and restore its SQL output with psql.
  • Roles and tablespaces: add a separate pg_dumpall --globals-only dump regardless of which database format you choose.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More quests from Patch Notes

  1. How to Set Up a RedM (RDR2) Server in 2026: License Key, txAdmin and server.cfg, Step by StepBlog11min
  2. Best RedM (RDR2) Server Hosting in 2026: Comparing Four Hosts on Slots, Memory and PriceBlog10min
  3. How to Host a Mindustry Server in 2026: server-release.jar, Port 6567 and the Commands That MatterBlog7min
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.