Copying one PostgreSQL database to staging with pg_dump, why do owner and grant statements fail on restore, and how do you fix that?
answer
- cluster-wide objects
- roles are not in the database dump
- pg_dumpall has a globals switch
- or drop owners and ACLs at restore
- --create carries database-level settings
basics
~10 spg_dump exports one database but not the cluster-wide roles its owners and grants name, so those statements fail on a fresh cluster. Restore the roles first with pg_dumpall --globals-only, or restore with --no-owner --no-privileges.
solid answer
~50 sRoles and tablespaces belong to the PostgreSQL **cluster**, not to a database, so `pg_dump` never writes them. Its dump still contains `ALTER ... OWNER TO harbor_app` and `GRANT ... TO harbor_ro`. On a staging cluster without those roles, each statement fails with `role "harbor_app" does not exist`. By default `pg_restore` logs each error, carries on, and reports `errors ignored on restore: N`. There are two fixes. You can bring the globals along: run `pg_dumpall --globals-only --no-role-passwords` from production, review the script, and run it with `psql` as a superuser before the restore. Or you can deliberately drop ownership and ACLs: `pg_restore --no-owner --no-privileges`, connected as the account that should own everything. Database-level settings (`ALTER DATABASE ... SET`, the database's own grants) travel only with `--create`. Add `--exit-on-error` so a missing role fails the job loudly instead of leaving a half-usable database.
code
bash · 12 lines# 1. Cluster-wide objects from production, without password hashes
pg_dumpall -h prod-pg -U postgres --globals-only --no-role-passwords -f globals.sql
psql -X -h staging-pg -U postgres -d postgres -f globals.sql
# 2. The customer database into an empty staging database
pg_dump -h prod-pg -U postgres -Fc -f cust_harbor.dump cust_harbor
createdb -h staging-pg -U postgres -T template0 cust_harbor
pg_restore -h staging-pg -U postgres -d cust_harbor --exit-on-error cust_harbor.dump
# Alternative to step 1: one staging owner, no source owners or grants
pg_restore -h staging-pg -U harbor_stage -d cust_harbor \
--no-owner --no-privileges --exit-on-error cust_harbor.dumpgo deeper
Remember that roles and tablespaces are cluster-wide, so pg_dump never contains them; pg_dumpall --globals-only does.
Walk through the failure: OWNER TO and GRANT statements naming missing roles, errors ignored by default, then the two fixes and what --create adds.
Show a repeatable staging refresh: globals without password hashes, a reviewed tablespace list, an exit-on-error restore, and a clear choice between real owners and one staging owner.
Decide whether staging should mirror production's role model, to test permissions, or flatten it to one owner, and weigh that against keeping credentials out of lower environments.
## Why a database dump is not self-contained A PostgreSQL **cluster** is one server instance holding several databases plus objects that belong to the cluster as a whole: **roles** (users and groups), **tablespaces**, and privileges granted on configuration parameters. `pg_dump` exports exactly one database. It therefore writes statements that *refer to* those global objects without ever creating them: - `ALTER TABLE crm.accounts OWNER TO harbor_app;` for every object's owner; - `GRANT SELECT ON TABLE crm.accounts TO harbor_ro;` for every privilege; - tablespace assignments for objects stored outside the default tablespace. Say you copy the customer database `cust_harbor` from production to a staging cluster that never had these roles. Every such statement then fails with `ERROR: role "harbor_app" does not exist`. By default, `pg_restore` (and `psql` replaying a plain script) logs the error and **carries on**. The restore appears to finish, but objects belong to whoever connected and the grants are missing. `pg_restore` ends with a warning such as `errors ignored on restore: 37`, which a cron job swallows. The application then fails later, with permission errors that look unrelated. ## Fix 1: copy the globals first `pg_dumpall --globals-only` (`-g`) writes the roles and tablespaces and no databases. `--roles-only` (`-r`) writes only the roles. The output is always a plain SQL script, and you run it with `psql` as a superuser before restoring the database: 1. `pg_dumpall -h prod-pg --globals-only --no-role-passwords -f globals.sql` 2. Review `globals.sql`. Remove roles staging should not have, and fix or remove `CREATE TABLESPACE` lines, whose directories must already exist on the target host. 3. `psql -X -h staging-pg -d postgres -f globals.sql`. Expect a harmless `role ... already exists` error for the bootstrap superuser, because the script issues `CREATE ROLE` for every role. 4. Restore the database. `--no-role-passwords` keeps production password hashes off staging. The roles are created without a password, so password authentication fails until you set one. It also reads `pg_roles` instead of `pg_authid`, which helps where access to `pg_authid` is restricted. ## Fix 2: restore without the source's owners and grants If staging should simply be owned by one account, leave out ownership and privileges: - `-O`/`--no-owner` emits no `ALTER ... OWNER` or `SET SESSION AUTHORIZATION`, so the connecting role owns everything it creates; - `-x`/`--no-privileges` (alias `--no-acl`) emits no `GRANT` or `REVOKE` statements; - `--role=rolename` issues `SET ROLE` after connecting, for setups where you log in as one role and work as another. With an archive, these are `pg_restore` options. With a plain dump they must be given to `pg_dump`, because the script is fixed when it is written. ## Settings that live on the database itself Even when all roles exist, `pg_restore -d cust_harbor` into a pre-created database misses whatever is attached to the database object rather than to objects inside it: - the database's comment; - `ALTER DATABASE ... SET` parameters; - `ALTER ROLE ... IN DATABASE ... SET` parameters; - the database's own `GRANT`s, such as `CONNECT`. Those are emitted only with `-C`/`--create`. With `-C`, the database named in `-d` is used only to issue `CREATE DATABASE`, and the data goes into the database name stored in the archive. If staging needs a different name, skip `-C`, create the target with `createdb -T template0 cust_harbor_stg`, and reapply database-level settings by hand. ## Make failures visible | Mode | pg_restore option | On the first error | |---|---|---| | default | none | log it, continue, report a count at the end | | stop | `-e`/`--exit-on-error` | exit; what was already restored stays | | all or nothing | `-1`/`--single-transaction` | roll everything back; implies `-e`, cannot be combined with `-j` or `-C` | In a scripted staging refresh, use `--exit-on-error` or check the error count, so that a missing role fails the job. Otherwise you silently get a database the application cannot use. ## Check before you restore An archive tells you which roles it expects before you run anything against staging: - `pg_restore -l cust_harbor.dump` prints one line per item, with its owner in the last column, so the set of owners is visible at a glance; - `pg_restore -f - cust_harbor.dump` prints the whole script, and searching it for `OWNER TO` and `GRANT` shows every role the restore will name; - comparing that list with the staging cluster's roles before the restore turns a late, silent failure into an early, obvious one.
- What does pg_restore --create bring that a restore into a pre-created database does not?`--create` also restores the database's comment, its `ALTER DATABASE ... SET` and `ALTER ROLE ... IN DATABASE ... SET` parameters, and the database's own privileges (unless `--no-acl` is given). The database named in `-d` is used only to issue `CREATE DATABASE`, and the data lands in the name stored in the archive. To restore under a different name, create the target from `template0` yourself and reapply those settings.
- Why add --no-role-passwords when copying production globals to staging?Without it, `pg_dumpall` writes every role's password hash, so production credentials spread to a less protected environment. With it, roles are created with no password, and password authentication fails until you set staging passwords. Because it then reads `pg_roles` instead of `pg_authid`, it also works where access to `pg_authid` is restricted.
- pg_dump includes the roles that own the dumped objects.
- pg_dumpall --globals-only also writes each database's schema.
- pg_restore stops at the first error by default.
- --no-owner keeps the original owners and only drops the GRANTs.
- ALTER DATABASE ... SET settings come with every pg_dump restore.