如何将转储从 PostgreSQL 合并到数据库中? [英] How to merge dump into database from PostgreSQL?

查看:103
本文介绍了如何将转储从 PostgreSQL 合并到数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在不同机器 (PostgreSQL) 上处理相同的数据库架构.我想知道,如何将数据从一台机器合并到另一台机器.Schema 有很多表(大约 10 个).我想达到什么目标?

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. 将数据从机器 A 转储到文件 A.dmp
  2. 将数据从文件 A.dmp 恢复到机器 B
  3. 当机器 B 中已经存在记录时,我不想将其插入机器 B.

我试图将数据从机器 A 转储到简单的 SQL 插入命令,但是当我尝试恢复它时,我收到重复的密钥错误.更重要的是,我想从命令行恢复数据(我必须导入 250 MB 数据),因为现在我正在尝试使用 pgAdmin 手动执行此操作.

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.

最好的方法是什么?

推荐答案

我终于做到了:

  1. 导出到转储:

  1. Export to dump with:

pg_dump -f dumpfile.sql --column-inserts -a -n <schema> -U <username> <dbname>

  • 为所有表设置唯一跳过

  • 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
    

  • 使用 psql

    \i <dumpfile.sql>
    

  • 这篇关于如何将转储从 PostgreSQL 合并到数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆