您的位置:首页 > 数据库

将数据从CSV文件导入PG数据库

2017-03-09 16:18 337 查看
csv文件内容:
[postgres@pg ~]$ vi PGOracle.csv

1,2,3
4,5,6
7,8,9

创建了新表:
postgres=# create table testim(col1 text,col2 text,col3 text);
CREATE TABLE

从文件导入数据:
postgres=# copy  testim from '/home/postgres/PGOracle.csv' CSV HEADER;
COPY 2

查看导入数据:
postgres=# select * from testim;
col1 | col2 | col3
------+------+------
4    | 5    | 6
7    | 8    | 9
(2 rows)

postgres=#

发现少了第一行,这是可预料的结果,因为指定了 CSV HEADER,此命令会忽略第一行。

但是发现去掉CSV HEADER会报错:
postgres=# copy  testim from '/home/postgres/PGOracle.csv' ;
ERROR:  missing data for column "col2"
CONTEXT:  COPY testim, line 1: "1,2,3"

那么以后做CSV导入的时候就要注意一下了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: