Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to backup some tables with data and some tables only schema PostgreSQL

I want to dump a database.

I have three tables:

table1 table2 table3

From table1 i want the schema plus data.

From table2 and table3 i just want the schema.

How do i do that?

like image 289
Diego Faria Avatar asked Jul 28 '11 13:07

Diego Faria


People also ask

How do I backup a specific table in PostgreSQL?

To backup a specific table, use the –table TABLENAME option in the pg_dump command. If there are same table names in different schema then use the –schema SCHEMANAME option. This is an example of backing up a specific Postgres database.

How do I export an entire schema in PostgreSQL?

Export schema structure using SQLYogFrom the Tools menu, choose Backup Database as SQL dump. At the top pane, choose Export as SQL: structure only. On the left side, choose the database to export. On the left side, uncheck all Object types except Tables.

Can a database have multiple schemas in PostgreSQL?

PostgreSQL supports having multiple schemas in a single database there by letting you namespace different features into different schemas. For example, you can have a database named postgres and have multiple schemas based on your application like ecommerce , auth etc.


2 Answers

To get data from just a few tables:

pg_dump myDatabase --inserts -a -t table1 -t table2 > backup.sql;

pg_dump myDatabase --inserts -a -t seq1 -t seq2 > backupSequences.sql;

Parameters descriptions:

-a, --data-only dump only the data, not the schema

-t, --table=TABLE dump the named table(s) only

--inserts dump data as INSERT commands, rather than COPY

This is what i wanted :)

Thanks all!

like image 148
Diego Faria Avatar answered Sep 23 '22 23:09

Diego Faria


Use pg_dump, which has both schema-only and schema + data output.

like image 23
atrain Avatar answered Sep 22 '22 23:09

atrain