oracle_fdw,file_fdw的安装与使用

转载请注明出处,谢谢~

简单的概括一下:oracle_fdw是pg用来访问oracle数据库的,类似于dblink,但功能更强大。file_fdw是pg用来创建外部表访问服务器上的文件。

file_fdw的安装与使用

  1. 源码安装方式
    先查找file_fdw
[root@localhost lib]# find / -name "file_fdw*"
/put/postgresql-12.5/contrib/file_fdw
/put/postgresql-12.5/contrib/file_fdw/file_fdw.c
/put/postgresql-12.5/contrib/file_fdw/input/file_fdw.source
/put/postgresql-12.5/contrib/file_fdw/file_fdw.control
/put/postgresql-12.5/contrib/file_fdw/output/file_fdw.source
/put/postgresql-12.5/contrib/file_fdw/file_fdw--1.0.sql

进入目录进行编译安装

[root@localhost lib]# cd /put/postgresql-12.5/contrib/file_fdw
[root@localhost file_fdw]# ll
total 48
drwxrwxrwx 2 1107 1107   119 Nov 10 06:39 data
drwxrwxrwx 2 1107 1107    24 Nov 10 06:39 expected
-rw-r--r-- 1 1107 1107   475 Nov 10 06:26 file_fdw--1.0.sql
-rw-r--r-- 1 1107 1107 35013 Nov 10 06:26 file_fdw.c
-rw-r--r-- 1 1107 1107   155 Nov 10 06:26 file_fdw.control
drwxrwxrwx 2 1107 1107    29 Nov 10 06:39 input
-rw-r--r-- 1 1107 1107   467 Nov 10 06:26 Makefile
drwxrwxrwx 2 1107 1107    29 Nov 10 06:39 output
drwxrwxrwx 2 1107 1107    24 Nov 10 06:39 sql

修改Makefile的PG_CONFIG
[root@localhost file_fdw]# vi Makefile
修改为PG_CONFIG = /home/postgres/pgsql/bin/pg_config 
#其中/home/postgres/pgsql为数据库安装目录

[root@localhost file_fdw]# make
[root@localhost file_fdw]# make install

出现以下结果就是成功了
在这里插入图片描述
接下来安装扩展

[root@localhost ~]# su - postgres
[postgres@localhost ~]$ cd /home/postgres/pgsql/bin
[postgres@localhost bin]$ ./psql
psql (12.5)
Type "help" for help.

postgres=# create extension file_fdw;
CREATE EXTENSION
  1. yum安装方式
    需要安装postgresql-contrib,比如我安装的是postgresql12,那么执行下面语句
[root@localhost ~]# yum -y install postgresql12-contrib

接下来就是安装扩展

[root@localhost ~]# su - postgres
[postgres@localhost ~]$ cd /home/postgres/pgsql/bin
[postgres@localhost bin]$ ./psql
psql (12.5)
Type "help" for help.

postgres=# create extension file_fdw;
CREATE EXTENSION
创建server:
postgres=# create server filefdw_server1 foreign data wrapper file_fdw;
CREATE SERVER

创建外部表

CREATE foreign TABLE employee(
  id int,
  name varchar(10),
  salary numeric(10),
  DepartmentId int
)SERVER filefdw_server1
OPTIONS (filename '/var/lib/pgsql/employee.csv', format 'csv');

file_fdw的使用方法网上一大堆,这里不再赘述。

oracle_fdw的安装与使用

安装Oracle Instant Client

  1. 下载:basic/sdk/sqlplus三个安装包,一定要与操作系统和位数(32bit或64bit)符合,我下载的是:
    instantclient-basic-linux.x64-12.2.0.1.0.zip
    instantclient-sdk-linux.x64-12.2.0.1.0.zip
    instantclient-sqlplus-linux.x64-12.2.0.1.0.zip
    下载地址
  2. 创建一个oracle客户端的目录/opt/oracle
  3. 将三个压缩包解压后,将所有文件直接拷贝到/opt/oracle/下面
  4. 在/opt/oracle/下面创建配置文件tnsname.ora(亲测tnsname.ora和tnsnames.ora都行)
    从其他安装了Oracle的服务器拷贝(用find查找一下,以我的为例/app/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora)
  5. oracle客户端环境变量的配置
[root@localhost ~]# vi /etc/profile
添加以下内容
export PG_HOME=/home/postgres/pgsql
export ORACLE_HOME=/opt/oracle
export SQLPATH=/opt/oracle
export TNS_ADMIN=/opt/oracle
export OCI_LIB_DIR=$ORACLE_HOME
export OCI_INC_DIR=$ORACLE_HOME/sdk/include
export LD_LIBRARY_PATH=$ORACLE_HOME:$LD_LIBRARY_PATH
export PATH=$PATH:$ORACLE_HOME

使上面的配置生效
[root@localhost ~]# source /etc/profile
  1. 创建oracle用户,并对客户端目录授权
[root@localhost ~]# groupadd oinstall
[root@localhost ~]# useradd -g oinstall -m oracle
[root@localhost ~]# passwd oracle
[root@localhost ~]# chown -R oracle:oinstall /opt/oracle
[root@localhost ~]# chmod -R 775 /opt/oracle
  1. 使用客户端进行连接测试
[root@localhost ~]# su - oracle
[oracle@localhost ~]$ cd /opt/oracle
[oracle@localhost oracle]$ sqlplus scott/password@192.168.253.131:1521/rpt #rpt为SID
当出现Connected to:xxxxxxx
SQL>  的时候说明oracle客户端配置完毕并连接成功。

源码编译安装oracle_fdw

  1. 下载源码并阅读安装说明
    官方下载和使用说明
    我下载的是oracle_fdw-2.3.1.zip
  2. 解压压缩包并配置Makefile
    查找pg_config位置(pg数据库安装目录的bin目录)
[root@localhost ~]# find / -name "pg_config"
/home/postgres/pgsql/bin/pg_config

更改oracle_fdw-2.3.1文件夹里的Makefile文件,指定pg_config位置

[root@localhost oracle_fdw-2.3.1]# grep PG_CONFIG Makefile
[root@localhost oracle_fdw-2.3.1]# vi Makefile
将PG_CONFIG=pg_config改为PG_CONFIG=$PG_HOME/bin/pg_config
  1. 编译并安装(必须是root用户下)
    直接在oracle_fdw-2.3.1目录下执行:
[root@localhost oracle_fdw-2.3.1]# make

当出现以下错误时

[root@localhost oracle_fdw-2.3.1]# make
make: G_HOME/bin/pg_config:命令未找到
make: *** 无目标。 停止。

解决办法:将Makefile中PG_CONFIG=$PG_HOME/bin/pg_config改为绝对路径,比如PG_CONFIG=/home/postgres/pgsql/bin/pg_config

继续make

/usr/bin/ld: cannot find -lclntsh
collect2: 错误:ld 返回 1
make: *** [oracle_fdw.so] 错误 1

解决方法:到oracle客户端的目录下,为libclntsh.so创建指向libclntsh.so.12.1的软链接
[root@localhost oracle_fdw-2.3.1]# cd /opt/oracle
[root@localhost oracle]# ln -s libclntsh.so.12.1 libclntsh.so

继续make
当没报错后执行make install

[root@localhost oracle_fdw-2.3.1]# make install
/usr/bin/mkdir -p '/home/postgres/pgsql/lib'
/usr/bin/mkdir -p '/home/postgres/pgsql/share/extension'
/usr/bin/mkdir -p '/home/postgres/pgsql/share/extension'
/usr/bin/mkdir -p '/home/postgres/pgsql/share/doc/extension'
/usr/bin/install -c -m 755  oracle_fdw.so '/home/postgres/pgsql/lib/oracle_fdw.so'
/usr/bin/install -c -m 644 .//oracle_fdw.control '/home/postgres/pgsql/share/extension/'
/usr/bin/install -c -m 644 .//oracle_fdw--1.2.sql .//oracle_fdw--1.0--1.1.sql .//oracle_fdw--1.1--1.2.sql  '/home/postgres/pgsql/share/extension/'
/usr/bin/install -c -m 644 .//README.oracle_fdw '/home/postgres/pgsql/share/doc/extension/'

出现以上信息说明orale_fdw编译安装完成。

  1. 安装扩展
以postgres用户登录
[postgres@localhost ~]$ cd /home/postgres/pgsql/bin
[postgres@localhost bin]$ ./psql
psql (12.5)
Type "help" for help.

postgres=# create extension oracle_fdw;
ERROR:  could not load library "/home/postgres/pgsql/lib/oracle_fdw.so": libclntsh.so.12.1: cannot open shared object file: No such file or directory

解决办法:创建软链接

[root@localhost ~]# su postgres
[postgres@localhost root]$ cd /home/postgres/pgsql/lib
[postgres@localhost lib]$ ldd oracle_fdw.so  #看下哪些动态库找不到
[postgres@localhost lib]$ ln -s /opt/oracle/libclntsh.so.12.1  /home/postgres/pgsql/lib/libclntsh.so.12.1

再次连上pg数据库

postgres=# create extension oracle_fdw;
ERROR:  could not load library "/home/postgres/pgsql/lib/oracle_fdw.so": libmql1.so: cannot open shared object file: No such file or directory

创建软链接后还是报同样错误。
终极武器:

[root@localhost oracle]# cd /etc/ld.so.conf.d/
[root@localhost ld.so.conf.d]# vi oracle-x86_64.conf
[root@localhost ld.so.conf.d]# more oracle-x86_64.conf 
/opt/oracle
[root@localhost ld.so.conf.d]# ldconfig
将oracle的库加入动态链接库,再次添加扩展即可

oracle_fdw的使用后续再更,网上的资源多得是。

感谢这几篇帖子的博主
https://my.oschina.net/liuyuanyuangogo/blog/500732
https://www.jianshu.com/p/e0d11f57ab75?tdsourcetag=s_pctim_aiomsg
http://www.mamicode.com/info-detail-2717319.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值