连接两个独立数据库的结果

发布于 2024-10-11 23:23:27 字数 92 浏览 1 评论 0原文

是否可以JOIN来自两个独立的postgres数据库的行?

我正在使用一台服务器上有几个数据库的系统,有时我真的需要这样的功能。

Is it possible to JOIN rows from two separate postgres databases?

I am working with system with couple databases in one server and sometimes I really need such a feature.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

独自←快乐 2024-10-18 23:23:27

根据 http://wiki.postgresql.org/wiki/FAQ

无法查询当前数据库以外的数据库。
因为 PostgreSQL 加载特定于数据库的系统目录,所以它是
甚至不确定跨数据库查询应该如何表现。
contrib/dblink 允许使用函数调用进行跨数据库查询。的
当然,客户端也可以同时连接到不同的
数据库并在客户端合并结果。

编辑:3年后(2014年3月),此常见问题解答条目已经过修订并且更有帮助:

如何使用多个数据库执行查询?

没有办法直接查询当前数据库以外的数据库
一。因为 PostgreSQL 加载特定于数据库的系统目录,所以它是
甚至不确定跨数据库查询应该如何表现。

PostgreSQL 中的 SQL/MED 支持允许“外部数据包装器”
创建,将远程数据库中的表链接到本地​​数据库。
远程数据库可能是同一 PostgreSQL 上的另一个数据库
实例,或者跨越半个地球的数据库,这并不重要。
postgres_fdw 内置于 PostgreSQL 9.3,包含读/写功能
支持; 9.2 的只读版本可以编译并安装为
贡献模块。

contrib/dblink 允许使用函数调用进行跨数据库查询
适用于更旧的 PostgreSQL 版本。与 postgres_fdw 不同
它无法将条件“下推”到远程服务器,因此它通常会
最终获取的数据比您需要的多得多。

当然,客户端也可以同时连接到
不同的数据库并在客户端合并结果。

According to http://wiki.postgresql.org/wiki/FAQ

There is no way to query a database other than the current one.
Because PostgreSQL loads database-specific system catalogs, it is
uncertain how a cross-database query should even behave.
contrib/dblink allows cross-database queries using function calls. Of
course, a client can also make simultaneous connections to different
databases and merge the results on the client side.

EDIT: 3 years later (march 2014), this FAQ entry has been revised and is more helpful:

How do I perform queries using multiple databases?

There is no way to directly query a database other than the current
one. Because PostgreSQL loads database-specific system catalogs, it is
uncertain how a cross-database query should even behave.

The SQL/MED support in PostgreSQL allows a "foreign data wrapper" to
be created, linking tables in a remote database to the local database.
The remote database might be another database on the same PostgreSQL
instance, or a database half way around the world, it doesn't matter.
postgres_fdw is built-in to PostgreSQL 9.3 and includes read/write
support; a read-only version for 9.2 can be compiled and installed as
a contrib module.

contrib/dblink allows cross-database queries using function calls and
is available for much older PostgreSQL versions. Unlike postgres_fdw
it can't "push down" conditions to the remote server, so it'll often
land up fetching a lot more data than you need.

Of course, a client can also make simultaneous connections to
different databases and merge the results on the client side.

っ左 2024-10-18 23:23:27

忘记 dblink!

Postgres_FDW问好:

准备使用 postgres_fdw 进行远程访问:

  1. 使用 CREATE EXTENSION 安装 postgres_fdw 扩展。

  2. 使用CREATE SERVER创建外部服务器对象,以表示您要连接的每个远程数据库。指定连接
    除用户和密码外的信息作为服务器的选项
    对象。

  3. 使用CREATE USER MAPPING为您想要允许访问每个外部服务器的每个数据库用户创建用户映射。指定
    用作用户和密码选项的远程用户名和密码
    用户映射。

  4. 使用 CREATE FOREIGN TABLEIMPORT FOREIGN SCHEMA 为您要访问的每个远程表创建一个外部表。列
    外部表的内容必须与引用的远程表匹配。你可以,
    但是,使用与远程不同的表和/或列名称
    表的,如果您指定正确的远程名称作为选项
    外部表对象。

现在您只需从外表中SELECT 即可访问数据
存储在其底层远程表中。

即使对于大数据它也非常有用。

Forget about dblink!

Say hello to Postgres_FDW:

To prepare for remote access using postgres_fdw:

  1. Install the postgres_fdw extension using CREATE EXTENSION.

  2. Create a foreign server object, using CREATE SERVER, to represent each remote database you want to connect to. Specify connection
    information, except user, and password, as options of the server
    object.

  3. Create a user mapping, using CREATE USER MAPPING, for each database user you want to allow to access each foreign server. Specify
    the remote user name and password to use as user and password options
    of the user mapping.

  4. Create a foreign table, using CREATE FOREIGN TABLE or IMPORT FOREIGN SCHEMA, for each remote table you want to access. The columns
    of the foreign table must match the referenced remote table. You can,
    however, use table and/or column names different from the remote
    table's, if you specify the correct remote names as options of the
    foreign table object.

Now you need only SELECT from a foreign table to access the data
stored in its underlying remote table.

It's really useful even on large data.

戒ㄋ 2024-10-18 23:23:27

是的,可以使用 dblink 来完成此操作,尽管需要考虑大量的性能问题。

以下示例将要求当前 SQL 用户对两个数据库都具有权限。如果 db2 不在同一个集群上,那么您需要将 dbname=db2 替换为 dblink 文档

SELECT * 
FROM   table1 tb1 
LEFT   JOIN (
   SELECT *
   FROM   dblink('dbname=db2','SELECT id, code FROM table2')
   AS     tb2(id int, code text);
) AS tb2 ON tb2.column = tb1.column;

如果 table2 非常大,则可能会出现性能问题,因为子查询在执行联接之前会加载整个 table2

Yes, it is possible to do this using dblink albeit with significant performance considerations.

The following example will require the current SQL user to have permissions on both databases. If db2 is not located on the same cluster, then you will need to replace dbname=db2 with the full connection string defined in the dblink documentation.

SELECT * 
FROM   table1 tb1 
LEFT   JOIN (
   SELECT *
   FROM   dblink('dbname=db2','SELECT id, code FROM table2')
   AS     tb2(id int, code text);
) AS tb2 ON tb2.column = tb1.column;

If table2 is very large, you could have performance issues because the sub-query loads up the entire table2 before performing the join.

-小熊_ 2024-10-18 23:23:27

只需几步,您就可以达到目标:
逐步遵循此参考< /a>

WE HAVE BEEN CONNECTED TO DB2 WITH TABLE TBL2 AND COLUMN COL2
ALSO THERE IS DB1 WITH TBL1 AND COLUMN COL1

 *** connecting to second db ie db2
    Now just **copy paste the 1-7 processes** (make sure u use correct username and password and ofcourse db name)

    1.**CREATE EXTENSION dblink;**

    2.**SELECT pg_namespace.nspname, pg_proc.proname 
    FROM pg_proc, pg_namespace 
    WHERE pg_proc.pronamespace=pg_namespace.oid 
       AND pg_proc.proname LIKE '%dblink%';**

    3.**SELECT dblink_connect('host=localhost user=postgres password=postgres dbname=db1');**

    4.**CREATE FOREIGN DATA WRAPPER postgres VALIDATOR postgresql_fdw_validator;**

    5.**CREATE SERVER postgres2 FOREIGN DATA WRAPPER postgres OPTIONS (hostaddr '127.0.0.1', dbname 'db1');**

    6.**CREATE USER MAPPING FOR postgres SERVER postgres2 OPTIONS (user 'postgres', password 'postgres');**

    7.**SELECT dblink_connect('postgres2');**

    ---Now, you can SELECT the data of Database_One from Database_Two and even join both db results:

    **SELECT * FROM public.dblink
    ('postgres2','SELECT col1,um_name FROM public.tbl1 ') 
    AS DATA(um_userid INTEGER),tbl2 where DATA.col1=tbl2.col2;**


You can also Check this :[How to join two tables of different databases together in postgresql [\[working finely in version 9.4\]][1]

Just a few steps and You can reach the goal:
follow this reference step by step

WE HAVE BEEN CONNECTED TO DB2 WITH TABLE TBL2 AND COLUMN COL2
ALSO THERE IS DB1 WITH TBL1 AND COLUMN COL1

 *** connecting to second db ie db2
    Now just **copy paste the 1-7 processes** (make sure u use correct username and password and ofcourse db name)

    1.**CREATE EXTENSION dblink;**

    2.**SELECT pg_namespace.nspname, pg_proc.proname 
    FROM pg_proc, pg_namespace 
    WHERE pg_proc.pronamespace=pg_namespace.oid 
       AND pg_proc.proname LIKE '%dblink%';**

    3.**SELECT dblink_connect('host=localhost user=postgres password=postgres dbname=db1');**

    4.**CREATE FOREIGN DATA WRAPPER postgres VALIDATOR postgresql_fdw_validator;**

    5.**CREATE SERVER postgres2 FOREIGN DATA WRAPPER postgres OPTIONS (hostaddr '127.0.0.1', dbname 'db1');**

    6.**CREATE USER MAPPING FOR postgres SERVER postgres2 OPTIONS (user 'postgres', password 'postgres');**

    7.**SELECT dblink_connect('postgres2');**

    ---Now, you can SELECT the data of Database_One from Database_Two and even join both db results:

    **SELECT * FROM public.dblink
    ('postgres2','SELECT col1,um_name FROM public.tbl1 ') 
    AS DATA(um_userid INTEGER),tbl2 where DATA.col1=tbl2.col2;**


You can also Check this :[How to join two tables of different databases together in postgresql [\[working finely in version 9.4\]][1]
揽月 2024-10-18 23:23:27

不,你不能。您可以使用 dblink 从一个数据库连接到另一个数据库,但是如果您正在寻找 JOIN,则不会有帮助。

您不能在单个数据库中使用不同的架构来存储所有数据吗?

No you can't. You could use dblink to connect from one database to another database, but that won't help if you're looking for JOIN's.

You can't use different SCHEMA's within a single database to store all you data?

鯉魚旗 2024-10-18 23:23:27

你需要使用 dblink ...正如上面提到的 araqnid ,这样的东西工作正常:

select ST.Table_Name, ST.Column_Name, DV.Table_Name, DV.Column_Name, *
from information_schema.Columns ST
full outer join dblink('dbname=otherdatabase','select Table_Name,
Column_Name from information_schema.Columns') DV(Table_Name text,
Column_Name text)
on ST.Table_Name = DV.Table_name
and ST.Column_Name = DV.Column_Name
where ST.Column_Name is null or DV.Column_Name is NULL

You need to use dblink...as araqnid mentioned above, something like this works fine:

select ST.Table_Name, ST.Column_Name, DV.Table_Name, DV.Column_Name, *
from information_schema.Columns ST
full outer join dblink('dbname=otherdatabase','select Table_Name,
Column_Name from information_schema.Columns') DV(Table_Name text,
Column_Name text)
on ST.Table_Name = DV.Table_name
and ST.Column_Name = DV.Column_Name
where ST.Column_Name is null or DV.Column_Name is NULL
剑心龙吟 2024-10-18 23:23:27

您已经使用了 postgresql 的 dblink 扩展。

参考本文:< /a>

PostgreSQL 的 DbLink 扩展,用于将一个数据库连接到另一个数据库。

安装 DbLink 扩展。

CREATE EXTENSION dblink;

验证DbLink:

SELECT pg_namespace.nspname, pg_proc.proname 
FROM pg_proc, pg_namespace 
WHERE pg_proc.pronamespace=pg_namespace.oid 
   AND pg_proc.proname LIKE '%dblink%';

我已经准备了对此的完整演示。请访问我的帖子来逐步学习在 Postgresql 中执行跨数据库查询。

You have use dblink extension of postgresql.

Reference take from this Article:

DbLink extension of PostgreSQL which is used to connect one database to another database.

Install DbLink extension.

CREATE EXTENSION dblink;

Verify DbLink:

SELECT pg_namespace.nspname, pg_proc.proname 
FROM pg_proc, pg_namespace 
WHERE pg_proc.pronamespace=pg_namespace.oid 
   AND pg_proc.proname LIKE '%dblink%';

I have already prepared full demonstration on this. Please visit my post to learn step by step for executing cross database query in Postgresql.

何其悲哀 2024-10-18 23:23:27

做不到吗? 当然可以,无需特殊扩展。在我们的例子中,我们必须比较来自不同数据库服务器的两个表,例如 ACC 和 PROD,因此比大多数答案更困难。特别是因为 ACC 和 PROD 故意位于不同的服务器上以形成屏障,因此您将无法轻松获得足够的权限来在外国服务器上执行 GRANT USAGE。

显而易见的解决方案是导出两个表,并以适当的名称(例如 table1_acc 和 table1_prod 或 acc 和 prod 等模式)将两个表导入到同一数据库(例如 DEV 或您自己的本地数据库)中。然后,您可以加入那些没有特殊问题的人。

Cannot be done? Of course we can, without special extensions. In our case, we had to compare two tables from different database servers, e.g. ACC and PROD, hence an even harder case than from most answers. Especially because ACC and PROD are deliberately on different servers to create a barrier, so you will not easily gain enough rights to perform a GRANT USAGE ON FOREIGN SERVER.

The obvious solution is to export both tables, and import both in the same database, e.g. DEV, or your own local db, under appropriate names, e.g. table1_acc and table1_prod, or schemas like acc and prod. Then, you may JOIN those with no special problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文