在postgresql端创建dblink链接:
-- Create the require functions for the FDW.
CREATE FUNCTION MysqL_fdw_handler()
RETURNS fdw_handler
AS '$libdir/MysqL_fdw'
LANGUAGE C STRICT;
CREATE FUNCTION MysqL_fdw_validator(text[],oid)
RETURNS void
AS '$libdir/MysqL_fdw'
LANGUAGE C STRICT;
-- Create the data wrapper or "transport".
CREATE FOREIGN DATA WRAPPER MysqL_fdw
HANDLER MysqL_fdw_handler
VALIDATOR MysqL_fdw_validator;
-- Create the foreign server,a pointer to the MysqL server.
CREATE SERVER MysqL_svr
FOREIGN DATA WRAPPER MysqL_fdw
OPTIONS (address '127.0.0.1',port '3306');
CREATE USER MAPPING FOR PUBLIC
SERVER MysqL_svr
OPTIONS(username 'vince',password '');
create foreign table test ( a integer,b text) server MysqL_svr options(table 'vince_db.test');
测试:
pgdb1=# select * from test;
a | b
---+---
1 | a
2 | b
(2 rows)
关联:
pgdb1=# create table MysqL_test(a integer,b text);
CREATE TABLE
pgdb1=# insert into MysqL_test(a,b) values(1,'a');
INSERT 0 1
pgdb1=# select * from test t1,MysqL_test t2 where t1.a=t2.a;
a | b | a | b
---+---+---+---
1 | a | 1 | a
(1 row)
插入:
pgdb1=# insert into test(a,b) values (3,'c');
ERROR: cannot change foreign table "test"
更新: