在mysql端创建用户和数据库:
mysql> create user vince;
Query OK,0 rows affected (0.00 sec)
mysql> create database vince_db ;
Query OK,1 row affected (0.00 sec)
mysql> grant all privileges on vince_db.* to 'vince'@'localhost' with grant option;
Query OK,0 rows affected (0.00 sec)
mysql> grant all privileges on vince_db.* to 'vince'@'%' with grant option;
Query OK,0 rows affected (0.00 sec)
mysql> use vince_db;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> create table test(a integer,b text);
Query OK,0 rows affected (0.00 sec)
mysql> insert into test(a,b)values (1,'a');
Query OK,1 row affected (0.00 sec)
mysql> insert into test(a,b)values (2,'b');
Query OK,1 row affected (0.00 sec)
在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"
更新: