Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Repair Windows errors before they cause bigger problems3Fix the driver behind crashes, sound loss and screen glitchesThe 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.
Antihost offers VPS plans with one-click Docker, NVMe storage and a 14-day free trial.
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.
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.
#1 Best Overall
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.
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
- Container:
postgresis the running container name. - Role:
-U myuserselects the PostgreSQL role used to read the database. - Database:
-d mydbselects the database being dumped. - Format:
-Fcrequests PostgreSQL’s custom archive format. - Host file:
> mydb.dumpsends 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.
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:
Recommended Free Tools
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.
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
--verboseprints detailed progress.--cleanremoves database objects before recreating them. Use it only when that destructive behavior is intended.--no-aclskips restoring access-control changes.--no-owneravoids 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.
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →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:
Best Value
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.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallUse 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.
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.
Quick Recap
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
- Identify the running PostgreSQL container or Compose service and the database, role, and host destination.
- Choose custom format for selective restoration, plain SQL for a readable single-database script, or
pg_dumpallfor all databases. - Run the dump inside the container with no TTY while redirecting output to the host.
- Run a separate
pg_dumpall --globals-onlycommand for roles, passwords, and tablespaces. - Keep the database dump and globals file together and record which PostgreSQL major version produced them.
- When recovery is needed, use
pg_restorefor custom archives andpsqlfor SQL scripts. - 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 bypg_restore. - One database as editable text: run
pg_dumpwithout-Fcand restore withpsql. - Several databases in one cluster: run
pg_dumpalland restore its SQL output withpsql. - Roles and tablespaces: add a separate
pg_dumpall --globals-onlydump 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.




