http://my.oschina.net/Kenyon/blog/214953
是一个类似dblink的一个跨库操作的中间件。
本地库:
ip:127.0.0.1
port:5432
dbname:postgres
外部库:ip:127.0.0.1
port:5001
dbname:digoal
本地库postgres信息:
postgres=# select * from pg_stat_activity;
-[ RECORD 1 ]----+--------------------------------
datid | 12896
datname | postgres
pid | 39391
usesysid | 10
usename | postgres
application_name | psql
client_addr | ::1
client_hostname |
client_port | 57313
backend_start | 2014-06-09 10:11:39.697793+08
xact_start | 2014-06-09 10:55:19.629726+08
query_start | 2014-06-09 10:55:19.629726+08
state_change | 2014-06-09 10:55:19.629749+08
waiting | f
state | active
query | select * from pg_stat_activity;
外部库digoal信息:
digoal=# select * from pg_stat_activity;
-[ RECORD 1 ]----+--------------------------------
datid | 16384
datname | digoal
pid | 39570
usesysid | 10
usename | postgres
application_name | psql
client_addr | ::1
client_hostname |
client_port | 42339
backend_start | 2014-06-09 10:51:28.05419+08
xact_start | 2014-06-09 10:52:46.52235+08
query_start | 2014-06-09 10:52:46.52235+08
state_change | 2014-06-09 10:52:46.522354+08
waiting | f
state | active
query | select * from pg_stat_activity;
CREATE table test ( id int,info text);
digoal=# insert into test values(1,'xxxx');
INSERT 0 1
CREATE SERVER server_name [ TYPE 'server_type' ] [ VERSION 'server_version' ] FOREIGN DATA WRAPPER fdw_name [ OPTIONS ( option 'value' [, ... ] ) ]
fdw_name:必须是creaete extension ...安装过的,比如“pgsql”,“file_fdw”,“plproxy”......
postgres=# create extension postgres_fdw ;
CREATE EXTENSION
postgres=# CREATE SERVER myserver FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'localhost', dbname 'digoal', port '5001');
CREATE SERVER
define a new mapping of a user to a foreign server
CREATE USER MAPPING FOR { user_name | USER | CURRENT_USER | PUBLIC } SERVER server_name [ OPTIONS ( option 'value' [ , ... ] ) ]user_name:本地数据库用户名。
server_name:是在上一步通过create server创建的名字。
options:远程数据库端的信息。
postgres=# CREATE USER MAPPING FOR postgres SERVER myserver OPTIONS (user 'postgres', password '***');
CREATE USER MAPPING
postgres=# \deu+
List of user mappings
-[ RECORD 1 ]------------------------------------
Server | myserver
User name | postgres
FDW Options | ("user" 'postgres', password '***')
CREATE FOREIGN TABLE test ( id int,info text) server myserver options (schema_name 'public', table_name 'test');
postgres=# select * from test;
id | info
----+------
1 | xxxx
(1 row)
postgres=# drop foreign table test;
DROP FOREIGN TABLE
postgres=# \deu+
List of user mappings
Server | User name | FDW Options
----------+-----------+-------------------------------------
myserver | postgres | ("user" 'postgres', password '***')
(1 row)
postgres=# DROP USER MAPPING IF EXISTS FOR postgres SERVER myserver;
DROP USER MAPPING
postgres=# drop server myserver ;
DROP SERVER