Read more

Merge two PostgreSQL dumps

Andreas Vöst
August 30, 2023Software engineer at makandra GmbH

Attention

This is an edge-case. You probably don't want to mix different database dumps. It also requires that the mixed tables do not share relations to other database objects.

Scenario

  • You've got two database dumps:
    • dump_a.pgdump
    • dump_b.pgdump
  • Both dumps include these tables:
    • foobar
    • barfoo
    • foobaz
  • You want to create a dump with:
    • dump_a.foobar
    • dump_b.barfoo
    • dump_a.foobaz

Invalid and incomplete restore

These commands might be obvious but won't actually work:

pg_restore --clean --if-exists --no-owner --no-privileges -d restore dump_a.pgdump
pg_restore --clean --if-exists --no-owner --no-privileges -d restore -t barfoo dump_b.pgdump
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

This will restore the table but won't restore depended objects like indices, primary keys, sequences as the pg_restore Show archive.org snapshot man page states:

When -t is specified, pg_restore makes no attempt to restore any other database objects that the selected table(s) might depend upon. Therefore, there is no guarantee that a specific-table restore into a clean database will succeed.

Valid restore

To archive a complete restore, first create a dump with only the tables you want to merge:

 pg_dump -Fc -t barfoo -f dump_b-barfoo.pgdump database

pg_dump Show archive.org snapshot will also dump all depended database objects.

Now you can create merge the two dumps with:

pg_restore --clean --if-exists --no-owner --no-privileges -d restore dump_a.pgdump
pg_restore --clean --if-exists --no-owner --no-privileges -d restore dump_b-barfoo.pgdump

Check

You can check the new database by comparing the output of \d+ in psql.

Andreas Vöst
August 30, 2023Software engineer at makandra GmbH
Posted by Andreas Vöst to makandra Operations (2023-08-30 09:02)