您可以将连接参数包装在@Peter 解释的FOREIGN SERVER
对象中,但您仍然必须拼出其余部分。
您可以将所有内容封装在视图或函数中,因此您只需键入一次。函数示例 - 以超级用户身份运行:
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 ....;