October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk8 min

How to Back Up a PostgreSQL Database Running in Docker (and Restore It Safely)

Run pg_dump inside the PostgreSQL container and redirect the stream to host storage. This guide covers custom and plain SQL formats, pg_dumpall globals, Docker Compose, version compatibility, restores, and failure fixes.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The dependable Docker pattern is to run pg_dump inside the running PostgreSQL container and redirect its standard output to a file on the host. For a portable archive, use -Fc and restore it with pg_restore; for a human-readable script, omit -Fc and restore with psql. Do not allocate a TTY while redirecting a dump, and create a separate globals dump when you need roles or tablespaces.

What you are backing up

pg_dump creates a logical backup of one PostgreSQL database. It contains that database’s schema and data, but not cluster-wide objects such as roles, role passwords, or tablespaces. Those objects belong to the PostgreSQL cluster and must be exported separately with pg_dumpall --globals-only.

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.

pg_dumpall can also produce one SQL script containing every database in the cluster. That is useful for a complete logical export, but it is a script for psql, not a custom archive for pg_restore.

The commands below assume a running container named postgres, a database named mydb, and a database user named myuser. Replace those example names with the names in your deployment. The redirection operator is deliberately outside docker exec: the shell running the command writes the file on the Docker host.

#1 Best Overall
Seagate 2TB Portable Hard Drive | USB 3.0 (STGX2000400) | Everyday HDD storage. Plug-and-play backup. Windows & macOS. Adobe CC trial included. 1-Year Warranty
  • Easily store and access 2TB to content on the go with the Seagate Portable Drive, a USB external hard drive
  • Designed to work with Windows or Mac computers, this external hard drive makes backup a snap just drag and drop
  • To get set up, connect the portable hard drive to a computer for automatic recognition no software required
  • This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable
  • The available storage capacity may vary.

Choose a dump format and scope

Method Command shape Output Restore with Best fit
Custom archive pg_dump -Fc Binary archive, not intended to be read as SQL pg_restore Selective object restores and flexible restore options
Plain SQL pg_dump without -Fc SQL text that can be inspected or edited psql Simple full-database restores and text-based workflows
All databases pg_dumpall One SQL script for the cluster psql A logical export of every database
Cluster globals pg_dumpall --globals-only Roles, passwords and tablespace definitions psql Restoring cluster-wide objects alongside a database dump

A custom archive is usually the most useful default when you may need to restore selected objects or use restore-time options. A plain SQL dump is preferable when you need a script that a person can read. Neither format is a Docker-volume snapshot; these procedures export PostgreSQL logically through its client utilities.

Check the container and client version first

  1. List running containers and confirm the database container name:

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
    docker ps
  2. Check the pg_dump version inside the container:

    docker exec postgres pg_dump --version
  3. Check the server version from the same container:

    docker exec postgres psql -U myuser -d mydb -c 'SELECT version();'

Use a pg_dump client that is the same major version as the server or newer. Running it inside the database container is a practical way to meet that requirement because the image normally includes client utilities aligned with the server image. The PostgreSQL 18 pg_dump documentation states that it can dump servers back to PostgreSQL 9.2, but an output archive is not guaranteed to load into an older major version. If you are intentionally moving to an older server, expect possible manual edits; the documentation recommends --quote-all-identifiers for cross-version dumps.

Authentication depends on the image and its pg_hba.conf configuration. The official PostgreSQL image commonly permits connections over the container’s local Unix socket without a password, but that is an observation about common configurations, not a guarantee. If your setup requires a password or a different host, use the credentials and connection options configured for that deployment rather than assuming the local-socket behavior.

Back up one database as a custom archive

Run the dump without a TTY

From the host, run:

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

docker exec starts pg_dump in the running container. The host shell receives the archive on standard output and writes mydb.dump in the current host directory. The -Fc option selects PostgreSQL’s custom format, which is designed for pg_restore.

Do not add -t or -it to a command whose output is redirected. A pseudo-terminal can alter line endings or allow diagnostic output to become mixed with the dump, producing an archive that cannot be restored reliably. A successful-looking file is not proof that the command completed successfully, so check the command’s exit status in your shell and investigate any error output.

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

Use a temporary filename for safer automation

For scheduled jobs, write to a temporary name and rename only after pg_dump succeeds. That prevents a failed or interrupted run from replacing the last known-good backup:

Rank #2
Seagate Portable 5TB External Hard Drive HDD – USB 3.0 for PC, Mac, PS4, & Xbox - 1-Year Rescue Service (STGX5000400), Black
  • Easily store and access 5TB of content on the go with the Seagate portable drive, a USB external hard Drive
  • Designed to work with Windows or Mac computers, this external hard drive makes backup a snap just drag and drop
  • To get set up, connect the portable hard drive to a computer for automatic recognition software required
  • This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable
  • The available storage capacity may vary.
docker exec postgres pg_dump -U myuser -d mydb -Fc > mydb.dump.part && mv mydb.dump.part mydb.dump

Store the resulting file on storage with enough free space for the archive. The dump is written on the host, not into the container’s writable layer.

Back up one database as plain SQL

For a text script, leave out -Fc:

docker exec postgres pg_dump -U myuser -d mydb > mydb.sql

The file can be opened as text and is restored through psql. The same no-TTY rule applies. In particular, do not copy a guide’s docker exec -t option into a redirected backup command simply because it appears in an example; a TTY is unnecessary here and can damage the stream.

Back up every database and the cluster globals

Export all databases in one SQL script

Run pg_dumpall inside the container and redirect its SQL output to the host:

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

This is a plain SQL script for psql. It is not a custom-format archive, so pg_restore cannot read it. The command covers all databases that the connected administrative user can access, but the cluster’s roles and tablespaces are still represented as cluster-wide statements and should be handled deliberately.

Export globals separately

If you are using individual pg_dump files, create a separate globals file:

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

Globals include roles, role passwords and tablespace definitions. A database dump does not contain them. Keep globals.sql with the database archive so a rebuild has the identity and ownership objects expected by the restored schema.

Restore a custom-format dump

Restore into an existing database

Create or select the target database, then stream the host archive into pg_restore running in the container:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Seagate Portable 1TB External Hard Drive HDD – USB 3.0 for PC, Mac, PlayStation, & Xbox, 1-Year Rescue Service (STGX1000400) , Black
  • Easily store and access 1TB to content on the go with the Seagate Portable Drive, a USB external hard drive.Specific uses: Personal
  • Designed to work with Windows or Mac computers, this external hard drive makes backup a snap just drag and drop. Reformatting may be required for Mac
  • To get set up, connect the portable hard drive to a computer for automatic recognition no software required
  • This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable
  • The available storage capacity may vary.
cat mydb.dump | docker exec -i postgres pg_restore -U myuser -d mydb

The -i option keeps standard input open; it is required because the archive is being piped into the container. A custom dump is not a script for psql; it must be processed by pg_restore.

Replace objects in the target

When the target database already contains an older copy, guides commonly use:

cat mydb.dump | docker exec -i postgres pg_restore --verbose --clean --no-acl --no-owner -U myuser -d mydb
  • --clean removes database objects before recreating them. Use it only when replacing the target is intended.
  • --no-acl skips access-control statements, which is useful when permissions will be managed separately.
  • --no-owner avoids restoring ownership statements that refer to roles absent from the target.
  • --verbose prints progress and object-level diagnostics.

If ownership and permissions must be reproduced exactly, restore the required roles and grants instead of automatically suppressing them. A globals restore is often the appropriate first step for a like-for-like cluster migration.

Restore roles and tablespaces before dependent objects

Apply the globals script as an administrative user:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
docker exec -i postgres psql -U postgres -f - < globals.sql

After the roles and tablespace definitions exist, restore the database archive. If the destination intentionally uses different roles, omit ownership and ACL statements and map permissions through your normal deployment process.

Restore plain SQL and pg_dumpall output

Restore a single-database SQL dump

Pipe the script to psql in the destination container:

docker exec -i postgres psql -U myuser -d mydb < mydb.sql

Here -i is appropriate because the SQL file is supplied through standard input. A plain SQL file is the format that psql executes directly.

Rank #4
Seagate Portable 4TB External Hard Drive HDD – USB 3.0, 1-Year Rescue | Everyday HDD storage. Plug-and-play backup. Windows & macOS. Adobe CC trial included. 1-Year Warranty
  • Easily store and access 4TB of content on the go with the Seagate Portable Drive, a USB external hard drive.Specific uses: Personal
  • Designed to work with Windows or Mac computers, this external hard drive makes backup a snap just drag and drop
  • To get set up, connect the portable hard drive to a computer for automatic recognition no software required
  • This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable
  • The available storage capacity may vary.

Restore an all-databases script

Use an administrative connection and feed the pg_dumpall output to psql:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
docker exec -i postgres psql -U postgres -f - < cluster.sql

The script contains statements for creating and loading databases, so perform this against a cluster where the connected role has the privileges required by the script. Do not pass cluster.sql to pg_restore; that utility expects an archive such as the custom format created with -Fc.

Docker Compose equivalent

With Docker Compose, use the service name instead of a container name. Compose allocates a TTY for exec by default, so disable it explicitly:

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

For a plain SQL file, keep the same -T option:

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

For restores, preserve standard input with -T and pipe the host file:

cat mydb.dump | docker compose exec -T db pg_restore -U myuser -d mydb

The service name db is only an example; use the database service name from your Compose file.

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

Common failures and precise fixes

“The input is a PostgreSQL custom-format dump” or similar errors from psql

You are feeding a -Fc archive to the SQL client. Restore it with pg_restore, not psql. Conversely, restore a plain SQL file or pg_dumpall output with psql.

The archive is corrupt or contains terminal escape characters

Re-run the backup without -t or -it. For Compose, include -T. A redirected dump must be a clean byte stream; a pseudo-terminal can change line endings or mix diagnostics into that stream.

Best Value
Sale
UnionSine 500GB Ultra Slim Portable External Hard Drive HDD-USB 3.0
  • [Upgraded Version] - This external hard drive features a mirrored logo stripe combined with a striped anti-slip design, and the rounded corners of the casing make it easier to grip. The stripes also have a heat dissipation function, ensuring stable and fast data transfer.
  • 【Ultra-thin and quiet】 - The motherboard adopts JMicron 578 noise-free solution, giving you a quiet working environment. Lightweight and portable size designed to fit in your pocket for easy portability.
  • 【Ultra-Fast Data Transfers】 - Pairing this external hard drive with JMicron 578 solution USB 3.0 and USB 2.0 interfaces enables blazing-fast data transfer. It boasts theoretical read speeds of up to 125MB/s and write speeds of up to 103MB/s.
  • 【Plug and Play】 - With no software to install, just plug it in and the drive is ready to use.The hard disk chip is wrapped with an aluminum anti-interference layer to increase heat dissipation and protect data.
  • 【What You Get】 - 1 x Portable Hard Drive, 1 x USB 3.0 Cable, 1 x User Manual, Gift-type shell packaging ,Three-year manufacturer's warranty and free technical support services.

“No such container” or “service not found”

Run docker ps and use the exact running container name with docker exec. With Compose, run the command against the service name, not the generated container name.

Authentication or permission errors

Confirm the database user, database name and connection method. Local-socket trust is common in the official PostgreSQL image but is not universal. A user may also lack permission to read every object being dumped or to create objects during restore; use the administrative account or grants appropriate to your environment.

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

“Role does not exist” during restore

The original role was not present in the destination. Restore globals.sql first, or repeat the custom-archive restore with --no-owner and, when appropriate, --no-acl. Suppressing ownership changes the resulting ownership, so decide which behavior you need.

Version-related errors

Check both versions with the commands in the version section. Use a pg_dump client at least as new as the server for the normal case. When moving between major versions, remember that newer clients can read older servers, but the generated output is not guaranteed to load into an older server. For a cross-version operation, test the restore against the exact destination version and consider --quote-all-identifiers.

The dump file is empty or unexpectedly small

Check the exit status immediately after the command and inspect error output. Verify that the database name is correct and that the connected user can access it. In automation, the temporary-file-and-rename pattern prevents an incomplete file from being mistaken for a successful backup.

Operational checklist

  • Confirm the container or Compose service is running.
  • Use a same-major-or-newer pg_dump client; running it inside the database container commonly provides that match.
  • Choose custom format (-Fc) when you need pg_restore features, or plain SQL when you need an editable script.
  • Never allocate a TTY for a redirected dump; use -T with Compose.
  • Write the output to host storage and use a temporary filename for scheduled jobs.
  • Export cluster globals separately with pg_dumpall --globals-only when roles or tablespaces matter.
  • Restore custom archives with pg_restore and SQL files with psql.
  • Test a restore against the destination version and account for ownership, ACL and role differences.

What this procedure does not cover

These commands are logical PostgreSQL exports. They do not describe copying a Docker data volume, taking a filesystem snapshot, or designing a physical-replication backup. A volume-level copy is a separate method with its own consistency and recovery requirements. If your recovery plan depends on such a copy, document and test that method independently rather than treating a pg_dump file as an interchangeable substitute.

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

Quick Recap

Bestseller No. 1
Seagate 2TB Portable Hard Drive | USB 3.0 (STGX2000400) | Everyday HDD storage. Plug-and-play backup. Windows & macOS. Adobe CC trial included. 1-Year Warranty
Seagate 2TB Portable Hard Drive | USB 3.0 (STGX2000400) | Everyday HDD storage. Plug-and-play backup. Windows & macOS. Adobe CC trial included. 1-Year Warranty
This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable; The available storage capacity may vary.
$129.99
Bestseller No. 2
Seagate Portable 5TB External Hard Drive HDD – USB 3.0 for PC, Mac, PS4, & Xbox - 1-Year Rescue Service (STGX5000400), Black
Seagate Portable 5TB External Hard Drive HDD – USB 3.0 for PC, Mac, PS4, & Xbox - 1-Year Rescue Service (STGX5000400), Black
This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable; The available storage capacity may vary.
Bestseller No. 3
Seagate Portable 1TB External Hard Drive HDD – USB 3.0 for PC, Mac, PlayStation, & Xbox, 1-Year Rescue Service (STGX1000400) , Black
Seagate Portable 1TB External Hard Drive HDD – USB 3.0 for PC, Mac, PlayStation, & Xbox, 1-Year Rescue Service (STGX1000400) , Black
This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable; The available storage capacity may vary.
$119.80
Bestseller No. 4
Seagate Portable 4TB External Hard Drive HDD – USB 3.0, 1-Year Rescue | Everyday HDD storage. Plug-and-play backup. Windows & macOS. Adobe CC trial included. 1-Year Warranty
Seagate Portable 4TB External Hard Drive HDD – USB 3.0, 1-Year Rescue | Everyday HDD storage. Plug-and-play backup. Windows & macOS. Adobe CC trial included. 1-Year Warranty
This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable; The available storage capacity may vary.
$208.99

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 from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

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.