Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2743831
  • 博文数量: 423
  • 博客积分: 7770
  • 博客等级: 少将
  • 技术积分: 4766
  • 用 户 组: 普通用户
  • 注册时间: 2006-11-09 11:58
个人简介

Oracle/DB2/Postgresql/Mysql/Hadoop/Greenplum/Postgres-xl/Mongodb

文章分类

全部博文(423)

文章存档

2019年(3)

2018年(6)

2017年(27)

2016年(23)

2015年(30)

2014年(16)

2013年(31)

2012年(73)

2011年(45)

2010年(14)

2009年(30)

2008年(30)

2007年(63)

2006年(32)

分类: Mysql/postgreSQL

2016-05-05 14:02:23

一、新建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=# 
阅读(2782) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~