PostgreSQL将数据从一个数据库复制/传输到另一个数据库 [英] PostgreSQL copy/transfer data from one database to another

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

问题描述

我需要将数据从一个表复制到另一个表。这两个表的结构几乎相同,但是位于不同的数据库中。

I need to copy data from one table to another. the two tables have almost the same structure, but are in different databases.

我尝试过

INSERT INTO db1.public.table2(
  id,
  name,
  adress,
  lat,
  lng
)
SELECT
  id,
  name,
  adress,
  lat
  lng
FROM db2.public.table2;

温恩我尝试这样做,我在跨数据库时出错...未实现

wenn i try this, i get error cross database ... not implemented

推荐答案

这是一个非常简单的任务。只需为此目的使用dblink:

This is a really straightforward task. Just use dblink for this purpose:

INSERT INTO t(a, b, c)
SELECT a, b, c FROM dblink('host=xxx user=xxx password=xxx dbname=xxx', 'SELECT a, b, c FROM t') AS x(a integer, b integer, c integer)

如果需要定期从外部数据库中获取数据,定义服务器和用户映射是明智的。然后,您可以使用较短的语句:

If you need to fetch data from external database on a regular basis, it would be wise to define a server and user mapping. Then, you could use shorter statement:

dblink('yourdbname', 'your query')

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

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