安装 dblink 扩展:
CREATE EXTENSION dblink;
安装 postgres_fdw 扩展(可用于访问存储在外部 PostgreSQL 服务器中的数据):
CREATE EXTENSION postgres_fdw;
创建一个新的外部服务器连接:
CREATE server myserver foreign data wrapper postgres_fdw
OPTIONS (dbname 'foreign_dbname', host 'foreign_host');
为您最近创建的外部服务器连接和数据库创建用户映射。
CREATE USER MAPPING FOR "user_in_current_database"
SERVER myserver OPTIONS (user 'foreign_user', password 'foreign_password');
选择创建了 conexion 的远程数据库中的一些字段。请注意,您不再需要使用用户名和密码。
SELECT tmp_table.*
FROM dblink(
'myserver',
'
SELECT field1,
field2
FROM table
'
)
AS tmp_table(
field1 TEXT,
field2 BIGINT
);
更多信息:
https://www.postgresql.org/docs/9.5/postgres-fdw.html https://www.postgresql.org/docs/9.5/postgres-fdw.html
https://www.postgresql.org/docs/current/sql-createserver.html https://www.postgresql.org/docs/current/sql-createserver.html
https://www.postgresql.org/docs/current/sql-createusermapping.html https://www.postgresql.org/docs/current/sql-createusermapping.html