简体   繁体   English

将旧数据库数据回填到现有 Postgres 数据库的最佳方法是什么?

[英]What is the best way to backfill old database data to an existing Postgres database?

A new docker image was recently stood up to replace an existing postgres database.最近建立了一个新的 docker 镜像来替换现有的 postgres 数据库。 A dump was taken of the database before the old instance was shut down using the following command:在使用以下命令关闭旧实例之前,对数据库进行了转储:

pg_dump -h localhost -p 5432 -d *dbname* -U postgres > *dbname*.pgdump

We'd like to concatenate or append this data to the new database in order to "backfill" some older historical data.我们希望将此数据连接或附加到新数据库中,以便“回填”一些较旧的历史数据。 The database name and schema of the two databases is identical.两个数据库的数据库名称和架构是相同的。 What is the easiest, safest way to do this?做到这一点最简单、最安全的方法是什么? Secondly, need postgres be shut down during the process?其次,在这个过程中需要关闭postgres吗?

If overlapping primary keys or unique columns have been assigned to the new data, then there will be no clean way to merge them without putting in some work to clean that up.如果已将重叠的主键或唯一列分配给新数据,那么将没有干净的方法来合并它们而不进行一些工作来清理它们。 Assuming that hasn't happened...假设没有发生...

The current dump file will have create statements for all the objects that already exists.当前转储文件将为所有已存在的对象创建语句。 If you replay that file into the current database, you will get a bunch of errors for all those objects.如果您将该文件重放到当前数据库中,您将收到所有这些对象的一堆错误。 If you don't have it all run in one transaction, then you could simply ignore those errors.如果您没有在一个事务中全部运行,那么您可以简单地忽略这些错误。 But, you might also load data in the wrong order and get foreign key violations.但是,您也可能以错误的顺序加载数据并导致外键违规。 Those errors will be mixed in with all the other ones about existing object, so might be easy to overlook.这些错误将与有关现有对象的所有其他错误混合在一起,因此可能很容易被忽视。

So what I would do is stand up an empty database server, and replay your current dump into that.所以我要做的是建立一个空的数据库服务器,然后将您当前的转储重播到其中。 Then retake the pg_dump, but with either -a or --section=data .然后重新获取 pg_dump,但使用-a--section=data Then you should be able to load that dump into your new database.然后您应该能够将该转储加载到您的新数据库中。 This has two advantages, it will not dump out CREATE statements which are not needed and throw errors which would need to be ignored, and it should dump the tables in an order which will not cause foreign key violations.这有两个优点,它不会转储不需要的 CREATE 语句并抛出需要忽略的错误,并且它应该以不会导致外键冲突的顺序转储表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 归档postgres数据库的最佳方法是什么? - What is the best way to archive a postgres database? 将现有数据库卷到容器的最佳方法是什么? - What is the best way to volume existing database to the container? 在Postgres的C#中获取表字段的数据库数据类型的最佳,最快的方法是什么? - What is the best and fast way to get database data type of table field in c# for Postgres? 在PostGres数据库中存储复选框的最佳方法是什么 - What's the best way to store checkboxes in a PostGres database 访问数据库中平均静态数据的最佳方法(Hibernate,Postgres) - Best way to access averaged static data in a Database (Hibernate, Postgres) 使用旧数据目录还原新的Postgres数据库 - Restoring New Postgres Database with Old Data Directory 处理庞大的 postgres 数据库的最佳方法 - Best way to deal with huge postgres database Rails 和现有的 Postgres 数据库 - Rails and Existing Postgres Database 在规范化数据库模式中访问数据的最佳方法是什么? - What is the best way to access data in a normalized database schema? 映射表时,将数据输入小型数据库的最佳方法是什么? - What is the best way to enter data into a small database when mapping tables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM