brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: pg_dump --exclude-table-data keeps a big log table's structure but skips its rows

Database
~/tips/tip-database-pg-dump-exclude-table-data

pg_dump -Fc --exclude-table-data='app_log*' -f appdb.dump appdb

Dumps a PostgreSQL database but leaves out the rows of chosen tables while keeping their definitions.

Some tables make a dump huge and are not worth keeping: sessions, logs, caches, job history. Leaving them out completely with -T means the restored database lacks the table and the app breaks. pg_dump has a better option for that case.

pg_dump -Fc --exclude-table-data='app_log*' -f appdb.dump appdb

The docs say --exclude-table-data skips the data of every table matching the pattern, while the table definition stays in the dump. You can repeat the option for more patterns. The docs describe it as useful when you need the definition of a table even though you do not need its data. The result is a dump where app_log exists after a restore, only empty.

pg_dump -Fc \
  --exclude-table-data='app_log*' \
  --exclude-table-data='sessions' \
  -f appdb.dump appdb

The pattern follows the same rules as -t, so the quotes stop your shell from expanding the star. The docs also list --exclude-table-data-and-children, which does the same and also covers partitions and inheritance children of the matching table.

The caveat is that this is a conscious choice to lose data. Anything you exclude is gone from that backup, so write down which tables you left out. A table that holds more than you think, such as a job queue with unfinished work, is not a table to exclude. If you want the structure of everything and no rows at all, --schema-only is the simpler switch.

Further reading: PostgreSQL documentation: pg_dump

More Database tips mariadb -e 'CREATE DATABASE IF NOT EXISTS blog' mariadb-dump --no-create-info --insert-ignore shop orders > orders-rows.sql SELECT table_schema, table_name, CHECKSUM TABLE shop.orders; services: All Database tips (53) All topics

Link