将结果从一个数据库中的PostgreSQL视图复制到另一个数据库中的表 [英] Copy results from a PostgreSQL view in one DB to a table in another

查看:874
本文介绍了将结果从一个数据库中的PostgreSQL视图复制到另一个数据库中的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

完成PostgreSQL newb。

Complete PostgreSQL newb.

我在db1中有7-8个视图值的数据,需要将它们复制到具有匹配模式(方案?)的表中。不同的数据库db2。目标数据库可以是PostgreSQL的相同实例,也可以是不同盒子上的一个实例。

I have 7-8 views worth of data in db1 that I need to copy into tables with a matching schemae (schemas?) in a different database, db2. The destination database may be the same instance of PostgreSQL, or one on a different box altogether.

我知道用2-3种不同的方法可以用我熟悉的数据库完成此任务,但是我对此很无奈。有人可以为我建议一些基本策略吗?

I know 2-3 different ways to accomplish this with the databases I'm familiar with, but I'm helpless on this one. Can someone suggest some basic strategies for me?

在一个完美的世界中,我宁愿不必做任何感觉像ETL一样的事情-我宁愿做某种

In a perfect world, I'd prefer not to have to do anything that feels too ETL-ish - I'd rather do some sort of

SELECT FROM instance1.db1.viewname INTO instance2.db5.tablename

然后将数据作为文本文件从视图中转出,然后重新加载到目标表中。

then dump data out of the view as text file and reload into the destination table.

但是,由于我不了解PostgreSQL,所以我并不真正知道可能性范围内的内容。

Since I don't know PostgreSQL, I don't really know what is within the realm of possibility, though.

推荐答案

您不需要为 COPY TO 创建临时表。自PostgreSQL 8.2起,任何查询都可以作为源。

You do not need to create a temporary table for COPY TO. Any query can be the source since PostgreSQL 8.2.

COPY (SELECT * FROM view1) TO '/var/lib/postgres/myfile1.csv';

阅读关于COPY的手册。在本地创建所需的表

Read the manual about COPY. Create the needed tables locally with

CREATE table tbl1 AS
SELECT * FROM view1
LIMIT 0;   -- no data, just the schema.

复制DDL指令并在目标数据库中创建所有表。 pgAdmin 是一种方便的GUI。再次删除源数据库中的空表。

Copy the DDL instructions and create all tables in the target db. pgAdmin is one convenient GUI to do that with. Delete the empty tables in the source db again. Load data with

COPY tbl1 FROM '/var/lib/postgres/myfile1.csv';

转储/恢复就像@wildplasser描述的那样,是另一种方式。

Dump / restore like @wildplasser describes it, is another way.

对于一次转移,建议使用其中一种方法。要重复使用,请 dblink SQL / MED (外部数据管理)

For a one time transfer one of those methods is advisable. For repeated application, dblink or SQL/MED (Management of External Data) may be more suitable.

这篇关于将结果从一个数据库中的PostgreSQL视图复制到另一个数据库中的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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