Sub topic 5
Backing Up Roles and Other Cluster-Wide Objects
Assume you know basic SQL, can use psql from a Linux shell, and understand PostgreSQL roles, databases, and the data directory. This chapter focuses on objects shared by a PostgreSQL cluster rather than tables and indexes inside one database.
Why database dumps are not enough
A logical database dump contains objects inside one database, such as:
- Tables, views, sequences, and functions
- Table data
- Database-level permissions
- Ownership information needed by those objects
It does not normally contain cluster-wide objects. These objects live above the database level:
- Roles, including login roles and passwords
- Role memberships
- Tablespace definitions
- Some cluster-wide configuration information
For example, a dump can contain a table owned by app_owner, but restoring that dump fails or changes ownership if app_owner does not exist on the target cluster.
pg_dumpall --globals-only creates a plain SQL file containing these global definitions. It does not dump table data or database contents.
Create a globals backup
Run the command as a role that can read the required catalog information:
pg_dumpall -U postgres --globals-only > globals.sqlThe -U postgres option selects the PostgreSQL role used to connect. The shell redirection writes SQL text to globals.sql.
Review the file before restoring it:
less globals.sqlYou may see statements such as:
CREATE ROLE app_owner;
ALTER ROLE app_owner WITH LOGIN PASSWORD '...';
CREATE ROLE app_readonly;
GRANT app_readonly TO app_user;
CREATE TABLESPACE fastspace OWNER postgres
LOCATION '/pgdata/fastspace';The file can include sensitive password hashes and filesystem paths. Protect it like a credential-containing backup.
Restore globals before databases
Restore the definitions into the target cluster before restoring individual database dumps:
psql -U postgres -d postgres -f globals.sqlpsql executes the SQL file while connected to the maintenance database postgres. The target database does not need to be the same name as the source database because roles and tablespaces belong to the cluster.
A typical restore order is:
- Install compatible PostgreSQL software.
- Create or prepare required tablespace directories.
- Restore roles and tablespaces.
- Create the target database.
- Restore the database's logical dump.
The dependency is important. A table dump may refer to a role that must already exist. A tablespace definition also requires its target directory to exist and have suitable ownership and permissions.
Handle environment differences
A tablespace stores its files outside the normal data directory. Its original path may not exist on the target server. Before restoring the globals file, check every CREATE TABLESPACE statement.
You may need to:
- Create the directory manually.
- Give it to the PostgreSQL operating-system user.
- Change the path in a reviewed copy of the SQL file.
- Omit a tablespace definition and restore objects into the default tablespace.
Role names and memberships also require review. A production restore should not blindly recreate administrative roles or reuse passwords in an untrusted environment.
For a restore that should stop immediately when one statement fails, use:
psql -U postgres -d postgres -v ON_ERROR_STOP=1 -f globals.sqlThis does not make the entire file transactional. Some role and tablespace operations cannot be safely rolled back as one transaction, so inspect the target after a failure.
Check what the backup contains
Search the file for important definitions:
grep -E '^(CREATE ROLE|ALTER ROLE|GRANT|CREATE TABLESPACE)' globals.sqlCompare the restored roles with the source:
\du
\dbRun those commands inside psql. \du lists roles and \db lists tablespaces.
Exercise
Why should you restore globals.sql before restoring a database dump?
Show answer
Database objects may refer to roles for ownership and permissions, and tables may use tablespaces. Restoring the global definitions first ensures those dependencies exist on the target cluster.