PostgreSQL Schema-only Backup
Published 26 November 2024
This document outlines how to create a schema-only backup of a PostgreSQL database.
Single database backup
The pg_dump command can be used to take a schema only backup of a single database, as the example below shows for the database named dbname:
$ pg_dump --schema-only dbname > backup.sql
It may be necessary to specify options for the host (-h) if connecting remotely, or the username (-U) to connect as a specific user.
Multi-database backup
The pg_dumpall command can be used to take a backup of all databases in a cluster, which can be useful if you want to backup multiple databases from the same PostgreSQL instance. The pg_dumpall also dumps global objects that are common to all databases, which are not included by pg_dump. A schema-only backup can be taken using the command shown below:
pg_dumpall --schema-only > backup.sql