在Postgres中使用dblink有什么快捷方式吗?

发布于 2024-12-11 01:43:25 字数 382 浏览 2 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(2

薯片软お妹 2024-12-18 01:43:25

在 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.

神妖 2024-12-18 01:43:25

您可以将连接参数包装在 FOREIGN SERVER 对象 就像 @Peter 解释,但你仍然需要拼写出来 其余的部分。

您可以将所有内容封装在视图或函数中,因此只需键入一次。函数示例 - 以超级用户身份运行:

CREATE OR REPLACE FUNCTION f_lnk_tbl()
  RETURNS TABLE(tbl_id int, col1 text, log_ts timestamp) AS
$BODY$

SELECT *
  FROM dblink(
  'SELECT tbl_id, col1, log_ts
   FROM   tbl
   ORDER  BY tbl_id'::text) AS b(
 tbl_id int
,col1   text
,log_ts timestamp);

$BODY$ LANGUAGE sql STABLE SECURITY DEFINER;
REVOKE ALL ON FUNCTION f_lnk_tbl() FROM public;


CREATE OR REPLACE FUNCTION f_sync()
  RETURNS text AS
$BODY$

SELECT dblink_connect('hostaddr=123.123.123.123 port=5432 dbname=mydb
                       user=postgres password=*secret*');

INSERT INTO my_local_tbl SELECT * FROM f_lnk_tbl();
-- more tables?

SELECT dblink_disconnect();

$BODY$
  LANGUAGE sql VOLATILE SECURITY DEFINER;
REVOKE ALL ON FUNCTION blob.f_dbsync() FROM public;
-- GRANT ....;

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:

CREATE OR REPLACE FUNCTION f_lnk_tbl()
  RETURNS TABLE(tbl_id int, col1 text, log_ts timestamp) AS
$BODY$

SELECT *
  FROM dblink(
  'SELECT tbl_id, col1, log_ts
   FROM   tbl
   ORDER  BY tbl_id'::text) AS b(
 tbl_id int
,col1   text
,log_ts timestamp);

$BODY$ LANGUAGE sql STABLE SECURITY DEFINER;
REVOKE ALL ON FUNCTION f_lnk_tbl() FROM public;


CREATE OR REPLACE FUNCTION f_sync()
  RETURNS text AS
$BODY$

SELECT dblink_connect('hostaddr=123.123.123.123 port=5432 dbname=mydb
                       user=postgres password=*secret*');

INSERT INTO my_local_tbl SELECT * FROM f_lnk_tbl();
-- more tables?

SELECT dblink_disconnect();

$BODY$
  LANGUAGE sql VOLATILE SECURITY DEFINER;
REVOKE ALL ON FUNCTION blob.f_dbsync() FROM public;
-- GRANT ....;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文