1. 安装file_fdw
需要先安装file_fdw,一般是进到PostgreSQL的源码包中的contrib/file_fdw目录下,执行:
make
make install
然后进入数据库中,执行以下SQL把file_fdw安装上:
CREATE EXTENSION file_fdw;
CREATE SERVER file_fdw_server FOREIGN DATA WRAPPER file_fdw;
create foreign table EMP
(
empno integer not null,
ename VARCHAR(10),
job VARCHAR(9),
mgr integer,
hiredate DATE,
sal NUMERIC(6,2),
comm NUMERIC(6,2),
deptno integer
)SERVER file_fdw_server
OPTIONS (format ‘csv‘,header ‘true‘,filename ‘/home/postgres/emp.log‘, delimiter ‘,‘, null ‘‘);
--后面options里面参数的说明
--filename后面是文件名和绝对路径
--format是格式,csv是逗号分隔,text表示是tab分隔的方式
--delimiter是分隔符--header表示第一行数据是否需要;为true表示不需要;默认为false;为需要
--null表示空数据的转化处理,例子中字段‘‘将转化为null
mydb=# select * from emp;
empno | ename | job | mgr | hiredate | sal | comm | deptno
-------+--------+-----------+------+------------+---------+---------+--------
7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | | 20
7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30
7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30
7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | | 20
7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30
7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | | 30
7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | | 10
7788 | SCOTT | ANALYST | 7566 | 1987-03-20 | 3000.00 | | 20
7839 | KING | PRESIDENT | | 1981-11-17 | 5000.00 | | 10
7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30
7876 | ADAMS | CLERK | 7788 | 1987-04-23 | 1100.00 | | 20
7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | | 30
7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | | 20
7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | | 10
(14 rows)