Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql: Backup and restore some tables with primary keys

Tags:

postgresql

I have a production Postgresql database which consist of 70 tables. Some of them are very big and some are small. And I have my local Postgresql database on my local machine. I want to make some of my local database tables's content be the same as production ones. If I just backup some tables with pgAdmin on production database and then try to restore on my local machine I got constrain errors. Because for example table A has foreign key to table B and so on.

How could I copy some tables from production database and restore normally on my local machine which has already scheme and tables and without constrain errors?

P.s. I couldn't just dump all production database because some of tables are VERY BIG.

like image 276
Eazy Avatar asked Aug 21 '12 11:08

Eazy


1 Answers

Dump complete production database, but without data in case of large tables:

$ pg_dump -t <VERY_BIG_TABLE_NAME> -s

If you want data also, avoid the -s option. Since you will have to repeat this 70 times, quicker solution is dividing tables into schemas:

$ pg_dump -n <SCHEMA_NAME_WITH_VERY_BIG_TABLES> -s
$ pg_dump -n <SCHEMA_NAME_WITH_SMALL_TABLES>
like image 80
Miljen Mikic Avatar answered Oct 18 '22 15:10

Miljen Mikic