POSTGRESQL

pg_dumpall: How to Dump All Your PostgreSQL Databases

intro

Let’s learn how to dump all PostgreSQL databases in a cluster using the pg_dumpall command-line utility!

pg_dumpall is a powerful PostgreSQL command-line tool used to export all databases in a cluster. That is the ideal solution to create complete database backups with a single command.

In this guide supported by detailed examples, you will see how to effectively use it and master PostgreSQL data management.

Let’s dive in!

What Is pg_dumpall?

pg_dumpall is a PostgreSQL command-line utility used to export ("dump") all databases within a PostgreSQL cluster into a single script. In this context, a “cluster” refers to a collection of databases managed by a single instance of a PostgreSQL server.

The output of pg_dumpall is a plain-text script containing SQL commands that can be fed into psql to recreate all the databases and their contents. Under the hood, pg_dumpall achieves this by internally calling pg_dump for each individual database in the cluster. Find out more in our complete guide on pg_dump.

In detail, pg_dumpall also exports global objects that are common across all databases. These include roles (users and groups), tablespaces, and any privilege grants related to global configuration parameters.

PostgreSQL pg_dumpall Command: Syntax and Options

The basic syntax of the PostgreSQL pg_dumpall command is:

Copy
        
1 pg_dumpall [options]

Where options include the flags to customize the behavior of the dump all logic, such as authentication details, output file format, compression, and more.

Notes:

  • pg_dumpall connects once per database and automatically prompts for a password if the server demands password authentication. Avoid that by using a ~/.pgpass file, which is also more secure as the typed password does not remain in terminal history.
  • Since pg_dump is used internally, some logs or errors will mention it. So, refer to it for debugging.
  • When importing a dump generated with pg_dumpall, you will likely see a harmless “role already exists” error for the bootstrap superuser. Use the -clean and -if-exists flags to reduce noise to hide those harmless errors.
  • Make sure all tablespace directories exist before restoring, or database creation may fail.

pg_dumpall Options

Below is a list of the most commonly used and important options for the PostgreSQL pg_dumpall command:

  • a, -data-only: Dump only the data, not the schema.
  • c, -clean: Include DROP commands to remove objects before recreating them.
  • f, -file: Output to a specified file instead of standard output.
  • g, -globals-only: Dump only global objects (roles and tablespaces), no databases.
  • O, -no-owner: Do not set ownership of objects in the dump; useful for restoring as another user.
  • r, -roles-only: Dump only roles, no databases or tablespaces.
  • s, -schema-only: Dump only the object definitions (schema), not data.
  • t, -tablespaces-only: Dump only tablespaces, no databases or roles.
  • v, -verbose: Enable verbose mode to get progress and timing info.
  • x, -no-privileges, -no-acl: ExcludeGRANT/REVOKE commands from the dump.
  • d, -dbname: Specify connection parameters via a connection string.
  • h, -host: Specify the database server host.
  • l, -database: Specify which database to connect to for dumping global objects.
  • p, -port: Specify the port or socket for the database connection.
  • -if-exists: Use DROP ... IF EXISTS statements to drop objects in -clean mode.
  • -no-comments: Avoid dumping database comments.
  • ?, -help: Show help for command-line arguments and exit.

For a complete list of pg_dumpall options, refer to the official documentation.

Note: Keep in mind that passing sensitive options via the command line can be insecure, as they may be visible to anyone with access to your shell history or process list.

pg_dumpall Environment Variables

These are the pg_dumpall environment variables:

  • PGHOST: Specifies the hostname or IP address of the PostgreSQL server. Use localhost for a local database.
  • PGPORT: Sets the port number for the PostgreSQL server (default: 5432).
  • PGUSER: Defines the default username used for authentication.
  • PGOPTIONS: Allows you to pass additional runtime options to the PostgreSQL server.
  • PG_COLOR: Controls whether color is used in diagnostic messages. The available options are always (always use color), auto (use color only when outputting to a terminal), and never (never use any color).

pg_dumpall Example List

Now that you know how the PostgreSQL cluster dumping utility works, get ready to explore a pg_dumpall example list!

Note: In the following examples, the user will always be named admin. Adjust this based on your specific user and database.

Dump All Databases into an SQL Script Format

To dump all databases in a PostgreSQL cluster into an SQL script format, use:

Copy
        
1 pg_dumpall -U admin -f all_databases_backup.sql

This will generate an all_databases_backup.sql file containing dumps for all databases.

Restore All Databases from a SQL Script

To restore the backup, run the following command:

Copy
        
1 psql -U admin -f all_databases_backup.sql

This command restores all the databases from the backup script.

Export Only Data

If you want to dump only the data (excluding schema), use the --data-only flag:

Copy
        
1 pg_dumpall -U admin --data-only -f all_databases_data.sql

This will export only the data in a format that contains COPY statements.

Export Only Schema

To export just the schema (structure of the databases), use the --schema-only flag:

Copy
        
1 pg_dumpall -U admin --schema-only -f all_databases_schema.sql

This will generate a SQL script containing only the CREATE and ALTER statements.

Dump a Remote Database Cluster Over SSH

To dump a PostgreSQL cluster remotely, you can use SSH tunneling to securely dump the entire cluster:

Copy
        
1 ssh user@remote_host "pg_dumpall -U admin" > remote_all_databases_backup.sql

Replace user@remote_host with the correct SSH user and host. This command will create a backup of the remote database cluster and save it locally.

Conclusion

In this guide, you learned what pg_dumpall is and the options it offers. This powerful PostgreSQL command-line utility enables you to back up all databases in a cluster. It provides many options, and here we highlighted the most important ones along with examples to help you get started.

Creating effective cluster dumps can be complex, and you might prefer a more advanced solution like DbVisualizer. Beyond offering the features of a robust database client, this tool allows you to export databases with just a few clicks.

DbVisualizer also includes advanced query optimization capabilities and can generate ERD-like schemas for better visualization of your database structure. Try DbVisualizer for free today!

FAQ

Where is the output of pg_dumpall saved?

By default, the output of pg_dumpall is printed to the standard output (i.e., the terminal or command prompt). However, you can redirect it to a file using the -f or --file option followed by a filename. That option enables you to save the dump to a file, which can then be used for backups or restoration purposes.

What is the path to pg_dumpall.exe?

On Windows, pg_dumpall.exe is typically located in the bin directory of your PostgreSQL installation. The path will generally look like this:

Copy
        
1 C:\Program Files\PostgreSQL\<version>\bin\pg_dumpall.exe

Simply replace <version> with the version number of your PostgreSQL server.

Does pg_dumpall require superuser privileges?

Since pg_dumpall reads tables from all databases, you will most likely need to connect as a database superuser to create a complete dump. Additionally, superuser privileges are required to execute the saved script, as you will need permission to add roles and create databases.

Is it possible to run pg_dumpall on a remote server?

First, make sure the PostgreSQL server is configured to allow remote connections. Then, run pg_dumpall from a remote server by following these steps:

  1. Use the h option to specify the hostname or IP address of the PostgreSQL server.
  2. Use the U option to specify the PostgreSQL username. Generally, you will also need to provide authentication details, such as a password. For secure connections, consider using SSH tunneling.

What is pg_dump and what is the difference with pg_dumpall?

pg_dump is a PostgreSQL utility used to create backups of individual databases. On the other hand, pg_dumpall backs up all databases in a PostgreSQL cluster, including global objects like roles and tablespaces.

Dbvis download link img
About the author
Antonello Zanini

Antonello is a software engineer, and often refers to himself as a technology bishop. His mission is to spread knowledge through writing.

The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

SQL Boolean Type: How to Use It in All Major Relational Databases

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2025-09-23
title

Postgres List Schemas: 3 Different Approaches

author Antonello Zanini tags POSTGRESQL 5 min 2025-09-15
title

pgvectorscale: An Extension for Improved Vector Search in Postgres

author Antonello Zanini tags AI POSTGRESQL Vectors 9 min 2025-09-03
title

PostgreSQL Index Mechanism: A Guide to Database Performance Optimization

author Leslie S. Gyamfi tags POSTGRESQL 8 min 2025-09-02
title

UUIDv7 in PostgreSQL 18: What You Need to Know

author Antonello Zanini tags POSTGRESQL 8 min 2025-09-01
title

PostgreSQL ISNULL Equivalent: COALESCE, CASE, and NULLIF

author TheTable tags POSTGRESQL 5 min 2025-08-19
title

SQL IS NOT NULL Condition: Definitive Guide

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-08-13
title

Can a Primary Key Be NULL in an SQL Table?

author TheTable tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 5 min 2025-08-12
title

ISNULL vs COALESCE: Comparing NULL Handling Functions

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-08-06
title

The SELECT INTO TEMP TABLE Mechanism in SQL

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-08-05

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.