连接两个独立数据库的结果
是否可以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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
根据 http://wiki.postgresql.org/wiki/FAQ
编辑:3年后(2014年3月),此常见问题解答条目已经过修订并且更有帮助:
According to http://wiki.postgresql.org/wiki/FAQ
EDIT: 3 years later (march 2014), this FAQ entry has been revised and is more helpful:
忘记 dblink!
向Postgres_FDW问好:
即使对于大数据它也非常有用。
Forget about dblink!
Say hello to Postgres_FDW:
It's really useful even on large data.
是的,可以使用 dblink 来完成此操作,尽管需要考虑大量的性能问题。
以下示例将要求当前 SQL 用户对两个数据库都具有权限。如果
db2
不在同一个集群上,那么您需要将dbname=db2
替换为 dblink 文档。如果
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 replacedbname=db2
with the full connection string defined in the dblink documentation.If
table2
is very large, you could have performance issues because the sub-query loads up the entiretable2
before performing the join.只需几步,您就可以达到目标:
逐步遵循此参考< /a>
Just a few steps and You can reach the goal:
follow this reference step by step
不,你不能。您可以使用 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?
你需要使用 dblink ...正如上面提到的 araqnid ,这样的东西工作正常:
You need to use dblink...as araqnid mentioned above, something like this works fine:
您已经使用了 postgresql 的 dblink 扩展。
参考本文:< /a>
PostgreSQL 的 DbLink 扩展,用于将一个数据库连接到另一个数据库。
安装 DbLink 扩展。
验证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.
Verify 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.
做不到吗? 当然可以,无需特殊扩展。在我们的例子中,我们必须比较来自不同数据库服务器的两个表,例如 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.