Heads up: pg_restore --clean keeps existing tables

Updated . Posted . Visible to the public. Repeats.

When restoring a PostgreSQL dump using pg_restore, you usually add the --clean flag to remove any existing data from tables.

Note that this only removes data from tables that are part of the dump and will not remove any extra tables. You need to do that yourself.

When is this relevant?

As an example: You want to load a staging dump into your development machine. On your development machine, you have run migrations that introduced more tables which do not yet exist on staging. pg_restore with --clean will load the dump successfully, i.e. restore data for all the tables it contained (including Rails' schema_migrations).
However, it would not remove any extra tables that were not part of the dump.
If you then try to run migrations locally, they will fail because of the existing tables.

Arne Hartherz
Last edit
Arne Hartherz
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2019-01-16 10:49)