166

I have a database with hundreds of tables, what I need to do is export specified tables and insert statements for the data to one sql file.

The only statement I know can achieve this is

pg_dump -D -a -t zones_seq interway > /tmp/zones_seq.sql

Should I run this statement for each and every table or is there a way to run a similar statement to export all selected tables into one big sql big. The pg_dump above does not export the table schema only inserts, I need both

Any help will be appreciated.

2 Answers 2

280

Right from the manual: "Multiple tables can be selected by writing multiple -t switches"

So you need to list all of your tables

pg_dump --column-inserts -a -t zones_seq -t interway -t table_3 ... > /tmp/zones_seq.sql  

Note that if you have several table with the same prefix (or suffix) you can also use wildcards to select them with the -t parameter:

"Also, the table parameter is interpreted as a pattern according to the same rules used by psql's \d commands"

Sign up to request clarification or add additional context in comments.

6 Comments

the -T switch is the same way but used to exclude tables. Might be useful if you need all but one or two tables etc.
The -D option seems to have disappeared in PG 9.1 onwards
The -D option was removed in version 8.4. You have to use --column-inserts now instead.
Try pg_dump --host 127.0.0.1 --port 5432 --username "<username>" --column-inserts --verbose --file "/tmp/<filename.sql>" --table "^<regex>*" "<tablename>"
Beware -a options skips tables schema export – so remove that option if you need to export these tables on fresh database
|
42

If those specific tables match a particular pattern, you can use that with the -t option in pg_dump.

pg_dump -D -a -t zones_seq -t interway -t "<pattern>" -f /tmp/zones_seq.sql <DBNAME>

For example to dump tables which start with "test", you can use

pg_dump -D -a -t zones_seq -t interway -t "^test*" -f /tmp/zones_seq.sql <DBNAME>

3 Comments

if it was a regex, shouldn't it be ^test.* instead of ^test*?
It's not a regex, it uses postgres's "patterns", so it should really be "test*": postgresql.org/docs/current/app-psql.html#APP-PSQL-PATTERNS
Not trivial to use regex, generating invalid regular expression: quantifier operand invalid error. But regular expression is correct... Only very simple regex are valid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.