一、新建file_fdw扩展模块
[postgres@db2 tmp]$ psql -d warehouse_db
psql (9.5.2)
Type "help" for help.
warehouse_db=# CREATE EXTENSION file_fdw;
CREATE EXTENSION
warehouse_db=# CREATE SERVER pgcvs FOREIGN DATA WRAPPER file_fdw;
CREATE SERVER
二、新建本地外部表
warehouse_db=# CREATE FOREIGN TABLE ghancsv(id integer,name varchar(20),departno integer,age integer) SERVER pgcvs
OPTIONS ( filename '/tmp/123.csv', format 'csv',header 'true' );
CREATE FOREIGN TABLE
四、注意设置中文编否则在windows编辑时乱码
warehouse_db=# \q
[postgres@db2 tmp]$ LANG=zh_CN.GBK
[postgres@db2 tmp]$ echo $LANG
zh_CN.GBK
五、将表数据导出成CSV格式带头文件
[postgres@db2 tmp]$ psql -d warehouse_db
psql (9.5.2)
Type "help" for help.
warehouse_db=# copy (select * from tanktab) to '/tmp/123.csv' with csv header;
COPY 5
warehouse_db=# select * from ghancsv;
id | name | departno | age
----+----------+----------+-----
1 | tank | 22 | 56
2 | 张国汉 | 22 | 56
2 | 张国汉 | 22 | 51
2 | 张国汉 | 22 | 51
2 | 张国模压 | 22 | 51
(5 rows)
六、测试CSV外部表
[postgres@db2 tmp]$ psql -d warehouse_db
psql (9.5.2)
Type "help" for help.
warehouse_db=# select * from ghancsv;
id | name | departno | age
----+----------+----------+-----
1 | tank | 22 | 56
2 | 张国汉 | 22 | 56
2 | 张国汉 | 22 | 51
2 | 张国汉 | 22 | 51
2 | 张国模压 | 22 | 51
3 | 他是中文 | 1 | 111
(6 rows)
warehouse_db=# create table tank as select * from ghancsv;
SELECT 6
warehouse_db=# select * from tank;
id | name | departno | age
----+----------+----------+-----
1 | tank | 22 | 56
2 | 张国汉 | 22 | 56
2 | 张国汉 | 22 | 51
2 | 张国汉 | 22 | 51
2 | 张国模压 | 22 | 51
3 | 他是中文 | 1 | 111
(6 rows)
warehouse_db=#
阅读(2846) | 评论(0) | 转发(0) |