我有一个包含 2550 列的 csv 文件,我想将其导入 postgreSQL。
为了在 postgreSQL 中导入 csv,首先我应该创建表,然后他们使用 \copy
从 csv 复制到表。但是,如果表中有大量列,如我的情况,我无法手动创建表怎么办。
有什么解决办法吗?
更新
数据结构如下: dZ(01) 到 dZ(2550) 基本上都在-50到+50之间:
id | date | time | localtime | pid | dZ(0)..dZ(1)......dZ(2550)|
---|---------|-----------|-----------|-----|---------------------------|
17|11-11-2014| 16:33:21 | 1.45E+15 |2375 |0 0 0 0 0 -1 0 -1 0 -5 -10|
CSV 结构:(我使用'';'分隔符)
17;13-11-2014;08:09:37;1.45E+15;4098;0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -4 3 0 -2 3 -2 1 0 0 1 1 3 -2 3 4 2 -2 -2 ....
这是一行数据。
最佳答案
将 dZ
列导入文本列,然后将其转换为数组:
创建临时表:
create table s (
id int,
date date,
time time,
localt double precision,
pid int,
dz text
);
设置日期样式:
set datestyle = 'DMY';
导入临时表;
\copy s from '/home/cpn/codigo/file.csv' with (format csv, delimiter ';')
创建最终表,将日期和时间合并为时间戳,并将 dZ
转换为数组:
create table t (
id int,
datetime timestamp,
localt double precision,
pid int,
dz integer[]
);
从临时填充确定的:
insert into t (id, datetime, localt, pid, dz)
select
id, date + time, localt, pid,
regexp_split_to_array(dz, '\s')::int[]
from s
关于postgresql - 将包含大量列的大 csv 导入 postgreSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43089525/