在进行postgresql 的查询中,如果我们想查询不在当前database中的table的话,可以通过在当前数据库中做一个映射,建立 FOREIGN TABLE ,则就可以查询远端数据库的表了,具体用法如下:
参考:http://www.postgresql.org/docs/9.3/static/postgres-fdw.html
https://wiki.postgresql.org/wiki/Foreign_data_wrappers
CREATE EXTENSION postgres_fdw;
注:删除命令 DROP EXTENSION postgres_fdw;
2.创建要访问的远端服务器(foreign server)
CREATE SERVER hisoka_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'localhost',port '5432',dbname 'postgres');
注:删除命令 DROP server hisoka_server;
3.创建对远端服务器访问控制的用户群组
CREATE USER MAPPING FOR public SERVER hisoka_server OPTIONS (user 'postgres',password 'welcome');
注:删除命令 DROP USER MAPPING FOR public SERVER hisoka_server;
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;
SELECT * FROM films;
最后再给一个参考地址:http://my.oschina.net/Kenyon/blog/214953?p=1