在进行postgresql 的查询中,如果我们想查询不在当前database中的table的话,可以通过在当前数据库中做一个映射,建立 FOREIGN TABLE ,则就可以查询远端数据库的表了,具体用法如下:@H_301_4@
参考:http://www.postgresql.org/docs/9.3/static/postgres-fdw.html
https://wiki.postgresql.org/wiki/Foreign_data_wrappers@H_301_4@
1.使用超级用户添加扩展(extension )@H_301_4@
CREATE EXTENSION postgres_fdw;
注:删除命令 DROP EXTENSION postgres_fdw;
@H_301_4@
2.创建要访问的远端服务器(foreign server)@H_301_4@
CREATE SERVER hisoka_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'localhost',port '5432',dbname 'postgres');
注:删除命令 DROP server hisoka_server;
@H_301_4@
CREATE USER MAPPING FOR public SERVER hisoka_server OPTIONS (user 'postgres',password 'welcome');
注:删除命令 DROP USER MAPPING FOR public SERVER hisoka_server;
@H_301_4@
CREATE FOREIGN TABLE films ( id serial NOT NULL,code character(5) NOT NULL,title character varying(40) NOT NULL,did integer NOT NULL,date_prod date,kind character varying(10) ) SERVER hisoka_server OPTIONS (schema_name 'public',table_name 'films');
注:删除命令 DROP FOREIGN TABLE films;
@H_301_4@
5.现在就可以访问或者修改远程数据库中的表了哈@H_301_4@
SELECT * FROM films;
最后再给一个参考地址:http://my.oschina.net/Kenyon/blog/214953?p=1@H_301_4@