Creating a database dump for specific tables and entries Postgres

Postgresql

Postgresql Problem Overview


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.

Postgresql Solutions


Solution 1 - Postgresql

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"

Solution 2 - Postgresql

If those specific tables match a particular regex, You can use the regex in -t option in pg_dump.

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

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionElitmiarView Question on Stackoverflow
Solution 1 - Postgresqla_horse_with_no_nameView Answer on Stackoverflow
Solution 2 - PostgresqlJothikanthView Answer on Stackoverflow