具有不同结构的两个数据库之间的数据迁移(Postgresql)

具有不同结构的两个数据库之间的数据迁移(Postgresql),postgresql,database-migration,Postgresql,Database Migration,我有两个数据库,old_db和new_db,我想做的是将数据记录从old_db传输到new db,但具有不同的结构或不同的列。我正在创建一个sql脚本,将旧的_db上传到新的_db,从那里我可以将数据从旧的_db上传到新的_db 旧数据库中的一个表如下所示: tbl_人员: person_id bigint, last_name text, first_name text, new_id bigint, ---this column is where the new id will be

我有两个数据库,old_db和new_db,我想做的是将数据记录从old_db传输到new db,但具有不同的结构或不同的列。我正在创建一个sql脚本,将旧的_db上传到新的_db,从那里我可以将数据从旧的_db上传到新的_db

旧数据库中的一个表如下所示:

tbl_人员:

person_id bigint,

last_name text,

first_name text,
new_id bigint, ---this column is where the new id will be generated

last_name text,

first_name text,

ref_id bigint; ---this column is where the person_id will be copied
现在,我想用如下结构将数据传输到新的\u db中,其中新的\u id列将生成新的id号,person\u id将被引用或传输到ref\u id列:

tbl_人员:

person_id bigint,

last_name text,

first_name text,
new_id bigint, ---this column is where the new id will be generated

last_name text,

first_name text,

ref_id bigint; ---this column is where the person_id will be copied

我如何创建一个sql脚本,以便将这些数据从旧的数据库正确引用到新的数据库???我不是要一个工具或GUI,而是一个sql脚本,我将在shell脚本中执行它。我正在使用postgresql作为我的DBMS,因此我还需要有关pg_转储或pg_还原的帮助,以便将旧的_数据库上载到新的_数据库中。TIA。

这项操作的根本目的是直接从旧表向新表插入数据。注意:我没有运行此代码

INSERT INTO new_db.tbl_person (last_name, first_name, ref_id)
(SELECT last_name, first_name, person_id FROM old_db.tbl_person)
如果两个数据库都在同一个Postgres实例中运行,那么这将独立工作。如果它们位于彼此可见的主机上的不同实例中,您可以使用,这使得
选择
查询类似于:

SELECT * FROM
dblink(
  'host=otherhost user=me password=pass dbname=old_db',
  'SELECT last_name, first_name, person_id FROM tbl_person'
) AS tbl_old_person(last_name text, first_name test, person_id integer)
如果主机彼此看不见,则在
pg_dump
pg_restore
上有很多关于StackOverflow的帮助:


这是一个过于笼统的问题。你能提供几个你真正想做的新旧参照的真实例子吗。我将使用“旧”和“新”引用查看“视图”,并使用这些“视图”替换“旧”表?例如:旧数据库(person_id,last_name,first_name)值(1234,Smith,John)|现在我想以生成新id的方式将此数据移动到新数据库,并在ref_id列中移动person_id,例如:new_db(new_id,last_name,first_name,ref_id)值(1,Smith,John,1234)我该怎么做???@RyanThank你。但我所做的是我将旧的_db上传到了新的_db。我想知道我该如何使用更新查询将旧的_db表中的数据更新到新的_db表中。@KristjanIt听起来你可能真的在寻找它。