在最低水平,你可以只使用COPY
命令(在psql
或\copy
如果你沒有獲得超級用戶帳戶,該帳戶COPY
需要從文件加載數據)。不幸的是,你必須首先創建表格結構(沒有內置的猜測標題特徵),但是看起來很簡單。
選擇適合您的您需要例如real
單精度浮點(IEEE 754),double precision
或numeric
,如果您需要任意精度的數字:
CREATE TABLE measurement
(
"Dist" double precision,
"Mv" double precision,
"CL" double precision,
"Typ" double precision,
"LTef" double precision,
"logg" double precision,
"Age" double precision,
"Mass" double precision,
"B-V" double precision,
"U-B" double precision,
"V-I" double precision,
"V-K" double precision,
"V" double precision,
"[Fe/H]" double precision,
"l" double precision,
"b" double precision,
"Av" double precision,
"Mbol" double precision
);
另一件事是,你的文件中包含的值之間的多個空格,所以最好把它改造成單-Tab分隔項(有很多工具來做到這一點):
$ sed 's/ */\t/g' import.csv
Dist Mv CL Typ LTef logg Age Mass B-V U-B V-I V-K V [Fe/H] l b Av Mbol
0.033 14.40 5 7.90 3.481 5.10 1 0.15 1.723 1.512 3.153 5.850 17.008 0.13 0.50000 0.50000 0.014 12.616
0.033 7.40 5 6.50 3.637 4.62 7 0.71 1.178 0.984 1.302 2.835 10.047 -0.56 0.50000 0.50000 0.014 6.125
0.052 11.70 5 7.40 3.529 4.94 2 0.31 1.541 1.167 2.394 4.565 15.393 -0.10 0.50000 0.50000 0.028 10.075
最後你可以導入你的文件直入Postgres數據庫,例如:
=> \copy measurement FROM '/path/import.csv' (FORMAT csv, DELIMITER E'\t', HEADER 'true')
=> TABLE measurement;
Dist | Mv | CL | Typ | LTef | logg | Age | Mass | B-V | U-B | V-I | V-K | V | [Fe/H] | l | b | Av | Mbol
-------+------+----+-----+-------+------+-----+------+-------+-------+-------+-------+--------+--------+-----+-----+-------+--------
0.033 | 14.4 | 5 | 7.9 | 3.481 | 5.1 | 1 | 0.15 | 1.723 | 1.512 | 3.153 | 5.85 | 17.008 | 0.13 | 0.5 | 0.5 | 0.014 | 12.616
0.033 | 7.4 | 5 | 6.5 | 3.637 | 4.62 | 7 | 0.71 | 1.178 | 0.984 | 1.302 | 2.835 | 10.047 | -0.56 | 0.5 | 0.5 | 0.014 | 6.125
0.052 | 11.7 | 5 | 7.4 | 3.529 | 4.94 | 2 | 0.31 | 1.541 | 1.167 | 2.394 | 4.565 | 15.393 | -0.1 | 0.5 | 0.5 | 0.028 | 10.075
(3 rows)