简体   繁体   English

使用查询将数据从一个db的表复制到另一个db的表中(两个表具有相同的结构)

[英]Copy data from table of one db into table of another db using query (both tables have same structure)

I have two database in the same schema. 我在同一架构中有两个数据库。 My db is in Postgres. 我的数据库是Postgres。 I want to copy data of any table (ie product) of my 1st db into the same table of the 2nd db. 我想将我的第一个数据库的任何表(即产品)的数据复制到第二个数据库的同一个表中。

Is it possible to do so using query? 是否可以使用查询这样做?

不能作为单个SQL命令(至少没有没有dblink),但最简单的方法可能是在两个psql之间使用管道 - 两端使用COPY,一个以CSV格式发送数据另一个收到它。

尝试

insert into db1.table1 select * from db2.table2

It's not possible in vanilla PostgreSQL installation. 在vanilla PostgreSQL安装中不可能。

If you are able to install contrib modules, use dblink : 如果您能够安装contrib模块,请使用dblink

INSERT
INTO    product
SELECT  *
FROM    dblink
        (
        'dbname=sourcedb',
        '
        SELECT  *
        FROM    product
        '
        ) AS p (id INT, column1 INT, column2 TEXT, …)

This should be run in the target database. 这应该在目标数据库中运行。

暂无
暂无

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

相关问题 是否可以使用某些查询将记录从一台服务器上的一个数据库表复制到另一台服务器上的另一数据库表? (两个表完全相同) - is it possible to copy records from one db table on one server to another db table on another server using some query? (both tables are exactly same) 仅将更新的表数据从一个数据库复制到另一数据库 - Copy only updated table data from one DB to another DB 将表数据从一个数据库复制到具有条件的另一个数据库 - copy table data from one db to another with where condition SQL:使用两个表中都存在的唯一键将数据从一个表复制到另一个表 - SQL: Copy data from one table into another using unique key present in both tables PSQL。 将数据从一个数据库迁移到具有不同表结构的另一个数据库 - PSQL. Migrating data from one DB to another DB with different table structure 如何将带有标识列及其值的表数据和结构复制到同一数据库中的另一个表(只需更改表名) - How to copy table data and structure with Identity column and its value to another table in the same db(just change table name) 将一个表的外键约束复制到mysql中同一个数据库中的另一个 - copy the foreign key constraints of one table to another in the same db in mysql 使用python在另一个数据库上创建一个相似的表 - Create a similar table from one DB on another DB using python 如何将数据和表结构从一个表复制到另一个表? - How to copy data and structure of table from one table to another? 将大型Nvarchar(max)表从一个数据库复制到另一个数据库 - copy large Nvarchar(max) table from one db to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM