covard
covard

Reputation: 1807

Exclude Table during pg_restore

UPDATE: Was able to exclude the data in the table durning the pg_dump command. Makes it even faster than trying to not load the data because you don't have to wait for that data to be dumped.

--exclude-table-data=event_logs

(PostgreSQL) 9.4.4

Anyone know how to exclude a table when doing a pg_restore? I can find how to do it when doing a pg_dump. However I am not the one doing the dump and can't exclude them.

There are 2 tables in the dump that are really big and take forever when I do a restore so I want to skip them.

Upvotes: 41

Views: 36152

Answers (4)

user3132194
user3132194

Reputation: 2567

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.

Update:

Starting from version 17 there is a --filter option in pg_dump, pg_dumpall and pg_restore tools.

Upvotes: 28

here the command did not work:

pg_restore -L restore.pgdump.list | psql

answered by Jesper Grann Laursen!

Here it worked by following the following sequence:

pg_restore -l $pgdump_file > restore.pgdump.list

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

pg_restore -v -L restore.pgdump.list -d dbname pgdump.file

Upvotes: 9

Jesper Grann Laursen
Jesper Grann Laursen

Reputation: 2367

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.

Upvotes: 79

e4c5
e4c5

Reputation: 53774

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.

Upvotes: 10

Related Questions