Exclude Table during pg_restore

TL;DR One-liner

pg_restore -L <(pg_restore -l /path/to/db/dump | grep -v 'TABLE DATA public table_to_ignore ') -d db_name_where_to_restore /path/to/db/dump

The following returns the "todo list" for a restore:

pg_restore -l /path/to/db/dump 

The following will return all except table_to_ignore (grep option -v makes it inverse the match):

pg_restore -l /path/to/db/dump | grep -v 'TABLE DATA public table_to_ignore '

This can be used in combination with pg_restore option -L which expects a input todo list:

pg_restore -L <(pg_restore -l /path/to/db/dump | grep -v 'TABLE DATA public table_to_ignore ') -d db_name_where_to_restore /path/to/db/dump

If you have several tables to ignore, you can the grep to:

pg_restore -l /path/to/db/dump | grep -vE 'TABLE DATA public (table_1 |table_2 |table_3 )'

Notice the presence of -E option for grep to use an extended regular expression.


I had the same problem. A long table list, and I want exclude the data from a few of the tables.

What I did was the following:

Run

pg_restore -l $pgdump_file > restore.pgdump.list

Open that restore.pgdump.list file in an editor, and insert an ; in front of the line saying

;2429; 0 27550 TABLE DATA public <table_to_explore> <database>

After saving the that file, it can now be used for importing, where all lines starting with ; are ignored.

pg_restore -L restore.pgdump.list | psql

You could make an one-liner to add ; in front of lines having a specific table name, if you completely want to ignore a specific table.

man pg_restore is also telling about this in an example in the end of the documentation.


pg_restore does not have an exclude table parameter, what it does have is an include table parameter.

-t table

--table=table

Restore definition and/or data of named table only. Multiple tables may be specified with multiple -t switches. This can be combined with the -n option to specify a schema.

If you have a large number of tables it does call for a litte bit of typing, but it does allow you to exclude specific tables by just leaving their names out of the list.