1. 简介
postgres_fdw扩展可以以外部表的方式在本地访问远程PG库里面的表。@H_502_5@
2. 使用postgres_fdw
测试环境是两个虚拟机。@H_502_5@
- 源库: 192.168.17.11 postgres
- 目标库: 192.168.17.35 postgres
postgres=# create extension postgres_fdw ;
CREATE EXTENSION
postgres=# \dx
List of installed extensions
Name | Version | Schema | Description
--------------+---------+------------+----------------------------------------------------
file_fdw | 1.0 | public | foreign-data wrapper for flat file access
hstore | 1.2 | public | data type for storing sets of (key,value) pairs
plpgsql | 1.0 | pg_catalog | PL/pgsql procedural language
postgres_fdw | 1.0 | public | foreign-data wrapper for remote Postgresql servers
(4 rows)
2.2 创建server@H_502_5@
postgres=# create server server_pgsql_fdw foreign data wrapper postgres_fdw options(host '192.168.17.35',port '5432',dbname 'postgres');
CREATE SERVER
2.3 create user mapping@H_502_5@
postgres=# create user mapping for public server server_pgsql_fdw options(user 'postgres',password '123456');
CREATE USER MAPPING
注意: create user mapping是将server与本地的用户连接起来。
public表示当前库的没有明确指定user mapping 的所有用户。@H_502_5@
2.4 创建外部表@H_502_5@
postgres=# create foreign table foreign_goods(id integer,name character varying)server server_pgsql_fdw options(schema_name 'public',table_name 'goods');
CREATE FOREIGN TABLE
2.5 查看数据@H_502_5@
postgres=# select * from foreign_goods;
id | name
----+------
1 | aa
2 | bb
3 | cc
(3 rows)
postgres=# select * from foreign_goods where id=1;
id | name
----+------
1 | aa
(1 row)
2.6 执行计划 @H_502_5@
postgres=# explain (analyze,verbose) select * from foreign_goods where id=1;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
Foreign Scan on public.foreign_goods (cost=100.00..127.20 rows=7 width=36) (actual time=1.256..1.257 rows=1 loops=1)
Output: id,name
Remote sql: SELECT id,name FROM public.goods WHERE ((id = 1)) Total runtime: 2.614 ms (4 rows)
3. 问题 @H_502_5@
3.1 查看数据@H_502_5@
postgres=# select * from foreign_goods ;
ERROR: could not connect to server "server_pgsql_fdw"
DETAIL: could not connect to server: No route to host
Is the server running on host "192.168.17.35" and accepting
TCP/IP connections on port 5432?
原因可能是:@H_502_5@
@H_502_5@
参考:
http://francs3.blog.163.com/blog/static/4057672720125711112496/
http://interdbconnect.sourceforge.net/pgsql_fdw/pgsql_fdw-en.html
http://my.oschina.net/Kenyon/blog/214953?p=1@H_502_5@