在Postgres中使用dblink有什么快捷方式吗?
在 Postgres 中,您可以使用 dblink 链接到其他数据库,但语法非常冗长。例如,您可以这样做:
SELECT *
FROM dblink (
'dbname=name port=1234 host=host user=user password=password',
'select * from table'
) AS users([insert each column name and its type here]);
有什么办法可以更快地做到这一点?也许预先定义连接?
我注意到 Postgres 有一个新的创建外部表
函数用于连接到 MySQL 数据库。它的语法比 dblink 更简单。我可以用那个吗?
In Postgres, you can link to your other databases using dblink
, but the syntax is very verbose. For example you can do:
SELECT *
FROM dblink (
'dbname=name port=1234 host=host user=user password=password',
'select * from table'
) AS users([insert each column name and its type here]);
Is there any way to do this faster? Maybe pre-define the connections?
I noticed that Postgres has a new create foreign table
function for connecting to a MySQL database. It has a simpler syntax than dblink
. Could I use that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 PostgreSQL 8.4 及更高版本中,您可以使用外部数据包装器功能来定义连接参数。然后,您只需在 dblink 命令中引用外部服务器名称即可。请参阅文档中的示例。
在 PostgreSQL 9.1 及更高版本中,您可以使用外部数据包装器功能来定义外部表,从而透明地访问远程数据库,而根本不需要使用 dblink。为此,您需要获取
postgresql_fdw
包装器,该包装器尚未包含在任何生产版本中,但您可以在互联网上找到实验代码。实际上,这条路线更多的是未来的选择。In PostgreSQL 8.4 and later, you can use the foreign data wrapper functionality to define the connection parameters. Then you'd simply refer to the foreign server name in your dblink commands. See the example in the documentation.
In PostgreSQL 9.1 and later, you can use the foreign data wrapper functionality to define foreign tables and thus access remote databases transparently, without using dblink at all. For that, you'd need to get the
postgresql_fdw
wrapper, which isn't included in any production release yet, but you can find experimental code in the internet. In practice, this route is more of a future option.您可以将连接参数包装在
FOREIGN SERVER
对象 就像 @Peter 解释,但你仍然需要拼写出来 其余的部分。您可以将所有内容封装在视图或函数中,因此只需键入一次。函数示例 - 以超级用户身份运行:
You can wrap connection parameters in a
FOREIGN SERVER
object like @Peter explains, but you'd still have to spell out the rest.You can encapsulate everything in a view or function, so you type it only once. Example with a function - Run as superuser: