Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dsouzajude/ac6cc637665e6f7c418edea351b6e3fc to your computer and use it in GitHub Desktop.
Save dsouzajude/ac6cc637665e6f7c418edea351b6e3fc to your computer and use it in GitHub Desktop.
Dump and import schema without indexes
# Read more about pg_dump, pre-data, post-data sections and the Fc flags at:
# https://www.postgresql.org/docs/13/app-pgdump.html
# -- Dump operations from Source Database --------------------------------------------
# Dumps the schema but excludes all the constraints and indexes (i.e pre-data section)
pg_dump -h $SOURCE_ENDPOINT --section=pre-data -U postgres -d $DB_NAME --file=schema-pre-data.dump.sql
# Dump the schema for the indices and constraints as well as we will need it later
# to apply it to the tables after the destination database is in sync.
pg_dump -h $SOURCE_ENDPOINT --section=post-data -Fc -Z 9 -U postgres -d $DB_NAME --file=schema-post-data.dump
# We need to extract out the primary keys from the post-data dump and import it into the
# destination database so that the logical replication works.
# Extract out the primary keys from the index and other constraints in a new list
pg_restore -l -Fc schema-post-data.dump | grep -v INDEX | grep -v TRIGGER | grep -v ACL > primary-keys.list
# Extract out the indexes in another list that we will use later when all the data is in sync
pg_restore -l -Fc schema-post-data.dump | grep -v CONSTRAINT > indexes-etc.list
# -- Import operations into the Destination (RDS) Database ---------------------------
# Import the pre-data section of the schema (excludes indexes and constraints)
psql -h $RDS_ENDPOINT --user postgres --dbname $DB_NAME < schema-pre-data.dump.sql
# Import the primary keys
pg_restore -h $RDS_ENDPOINT --user postgres --dbname $DB_NAME -v -Fc -L primary-keys.list schema-post-data.dump
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment