1. 程式人生 > >PostgreSQL外掛:postgres_fdw 編譯安裝使用

PostgreSQL外掛:postgres_fdw 編譯安裝使用

文章目錄

概述

所述postgres_fdw模組提供外國資料封裝器postgres_fdw,其可用於訪問儲存在外部資料的PostgreSQL伺服器。

此模組提供的功能與舊版dblink模組的功能基本重疊。但postgres_fdw為訪問遠端表提供了更透明和符合標準的語法,並且在許多情況下可以提供更好的效能。

編譯外掛

postgres_fdw 是PG原始碼包自帶的外掛,所以直接去原始碼包進行編譯安裝即可,這裡需要注意的是,在編譯的時候,最好是用postgres使用者來執行操作
如下:

[[email protected]_PG_10 postgres_fdw]$ pwd
/opt/postgresql-10.6/contrib/postgres_fdw
[[email protected]_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

執行編譯安裝

[[email protected]_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=# 

查看錶

postgres=# select * from pg_fdw_test ;
 id | name 
----+------
  1 | aaa
  2 | bb
(2 rows)