1.创建相关函数
CREATE OR REPLACE FUNCTION "public"."postgres_fdw_handler"() RETURNS "pg_catalog"."fdw_handler" AS '$libdir/postgres_fdw', 'postgres_fdw_handler' LANGUAGE 'c' VOLATILE STRICT COST 1; ALTER FUNCTION "public"."postgres_fdw_handler"() OWNER TO "postgres"; CREATE OR REPLACE FUNCTION "public"."postgres_fdw_validator"(_text, oid) RETURNS "pg_catalog"."void" AS '$libdir/postgres_fdw', 'postgres_fdw_validator' LANGUAGE 'c' VOLATILE STRICT COST 1; ALTER FUNCTION "public"."postgres_fdw_validator"(_text, oid) OWNER TO "postgres";
2.创建外部数据封装器
CREATE FOREIGN DATA WRAPPER postgres_fdw HANDLER postgres_fdw_handler VALIDATOR postgres_fdw_validator; ALTER FOREIGN DATA WRAPPER postgres_fdw OWNER TO postgres;
3.创建外部服务器
CREATE SERVER smp_fdw FOREIGN DATA WRAPPER postgres_fdw OPTIONS ( host '172.18.1.4', dbname 'SMP', port '5432' ); ALTER SERVER smp_fdw OWNER TO postgres;
4.创建用户
create user mapping FOR PUBLIC server smp_fdw options (password 'xxx');
5.创建外部表
create foreign table test (id int, name varchar) server smp_fdw options (table_name 'test');
6.查询
select * from test
本文链接地址: PSQL建立外部表