概述
所述postgres_fdw模块提供外国数据封装器postgres_fdw,其可用于访问存储在外部数据的PostgreSQL服务器。
此模块提供的功能与旧版dblink模块的功能基本重叠。但postgres_fdw为访问远程表提供了更透明和符合标准的语法,并且在许多情况下可以提供更好的性能。
编译插件
postgres_fdw 是PG源码包自带的插件,所以直接去源码包进行编译安装即可,这里需要注意的是,在编译的时候,最好是用postgres用户来执行操作
如下:
[postgres@WAhaha_PG_10 postgres_fdw]$ pwd
/opt/postgresql-10.6/contrib/postgres_fdw
[postgres@WAhaha_PG_10 postgres_fdw]$ ls
connection.c expected option.c postgres_fdw.c postgres_fdw.h sql
deparse.c Makefile postgres_fdw--1.0.sql postgres_fdw.control shippable.c
执行编译安装
[postgres@WAhaha_PG_10 postgres_fdw]$ USE_PGX=1 make install
make -C ../../src/interfaces/libpq all
make[1]: Entering directory `/opt/postgresql-10.6/src/interfaces/libpq'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/opt/postgresql-10.6/src/interfaces/libpq'
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC -I../../src/interfaces/libpq -I. -I. -I../../src/include -D_GNU_SOURCE -I/usr/include -c -o postgres_fdw.o postgres_fdw.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC -I../../src/interfaces/libpq -I. -I. -I../../src/include -D_GNU_SOURCE -I/usr/include -c -o option.o option.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC -I../../src/interfaces/libpq -I. -I. -I../../src/include -D_GNU_SOURCE -I/usr/include -c -o deparse.o deparse.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC -I../../src/interfaces/libpq -I. -I. -I../../src/include -D_GNU_SOURCE -I/usr/include -c -o connection.o connection.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC -I../../src/interfaces/libpq -I. -I. -I../../src/include -D_GNU_SOURCE -I/usr/include -c -o shippable.o shippable.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC -shared -o postgres_fdw.so postgres_fdw.o option.o deparse.o connection.o shippable.o -L../../src/port -L../../src/common -L../../src/interfaces/libpq -lpq -L/usr/lib64 -Wl,--as-needed -Wl,-rpath,'/usr/pgsql-8.4/lib',--enable-new-dtags
/usr/bin/mkdir -p '/usr/pgsql/lib'
/usr/bin/mkdir -p '/usr/pgsql/share/extension'
/usr/bin/mkdir -p '/usr/pgsql/share/extension'
/usr/bin/install -c -m 755 postgres_fdw.so '/usr/pgsql-8.4/lib/postgres_fdw.so'
/usr/bin/install -c -m 644 ./postgres_fdw.control '/usr/pgsql-8.4/share/extension/'
/usr/bin/install -c -m 644 ./postgres_fdw--1.0.sql '/usr/pgsql-8.4/share/extension/'
其实这个过程就是使用pg_config的环境,将编译好的.so文件拷贝到lib动态库中,.control文件放在extension 文件中
安装使用
创建拓展和server,server里
postgres=# create extension postgres_fdw ;
CREATE EXTENSION
postgres=# create server postgres_fdwtest FOREIGN data wrapper postgres_fdw OPTIONS(host '192.168.6.21', port '5432', dbname 'postgres');
CREATE SERVER
postgres=# \desc
List of foreign servers
Name | Owner | Foreign-data wrapper
------------------+----------+----------------------
postgres_fdwtest | postgres | postgres_fdw
(1 row)
创建用户mapping
postgres=# create user mapping for postgres server postgres_fdwtest options(user 'postgres',password 'postgres');
CREATE USER MAPPING
postgres=#
建好test用户表
postgres=# create foreign table pg_fdw_test(id int,name text) server postgres_fdwtest options (table_name 'test');
CREATE FOREIGN TABLE
postgres=#
查看表
扫描二维码关注公众号,回复:
4780653 查看本文章
postgres=# select * from pg_fdw_test ;
id | name
----+------
1 | aaa
2 | bb
(2 rows)