Is there any shortcut for using dblink in Postgres?

安稳与你 提交于 2019-11-30 14:43:46

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.

Erwin Brandstetter

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 ....;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!