file_fdw,可以访问服务器的文件系统的数据,或者执行程序在服务器上,并且读取输出。
数据文件或程序输出必须以可从副本读取的格式。对数据文件的访问是只读的。
环境:
CentOS 7
PG 10.4
准备:
1. 外部表文件
[root@dbwtest03bc data]# cat ft_dir/ft_test01.csv
1,aaa,aaa
2. 文件及文件夹权限
chown -R postgres:postgres ft_dir
创建外部表:
1. create extension
CREATE EXTENSION file_fdw;
mytest=# create extension file_fdw ;
CREATE EXTENSION
2. create a foreign server:
mytest=# CREATE SERVER ft_test01 FOREIGN DATA WRAPPER file_fdw;
CREATE SERVER
3. 创建外部表
mytest=# CREATE FOREIGN TABLE ft_test01 (
mytest(# id int,
mytest(# col1 text,
mytest(# col2 text
mytest(# ) SERVER ft_test01
mytest-# OPTIONS ( filename '/data/ft_dir/ft_test01.csv', format 'csv' );
CREATE FOREIGN TABLE
4. 查看数据
mytest=# select * from ft_test01 ;
id | col1 | col2
----+------+------
1 | aaa | aaa
(1 row)
5. 外部表只读,无法插入,更新,删除
mytest=# update ft_test01 set col1 = 'aaa' , col2 = 'bbb' where id =1 ;
ERROR: cannot update foreign table "ft_test01"
mytest=# insert into ft_test01 values (2,'bbb','ccc');
ERROR: cannot insert into foreign table "ft_test01"