How to merge dump into database from PostgreSQL?

雨燕双飞 提交于 2019-12-03 16:56:20

问题


I'm working on the same databse schema on different machines (PostgreSQL). I would like to know, how to merge data from one machine to another. Schema has many tables (around 10). What I want to achieve?

  1. Dump data from machine A to file A.dmp
  2. Restore data from file A.dmp to machine B
  3. When a record already exists in machine B, I would like to don't insert it into machine B.

I was trying dump data from machine A to simple SQL inserts commands, but when I'm trying to restore it, I am getting duplicate key errors. What is more, I would like to restore data from command line (I have to import 250 MB data), because now I'm trying to do it manually with pgAdmin.

What is the best way to do it?


回答1:


I finally did it this way:

  1. Export to dump with:

    pg_dump -f dumpfile.sql --column-inserts -a -n <schema> -U <username> <dbname>
    
  2. Set skip unique for all tables

    CREATE OR REPLACE RULE skip_unique AS ON INSERT TO <table>
        WHERE (EXISTS (SELECT 1 FROM <table> WHERE users.id = new.id)) 
        DO INSTEAD NOTHING
    
  3. Import with psql

    \i <dumpfile.sql>
    


来源:https://stackoverflow.com/questions/20187947/how-to-merge-dump-into-database-from-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!