bag*_*nte 7 postgresql import datetime date csv
我有一长串.csv
文件,我想将其导入本地数据库。DATE
我相信我的查询是正确的,但解析和列存在一些问题TIMESTAMP
。PostgreSQL 读取这些列时需要 ISO 格式“yyyy/mm/dd”,但我的数据采用另一种格式:“dd/mm/yyyy”。
我在网上和其他 Stack Overflow 答案上读到,可以有所SET
不同datestyle
,但不建议这样做。
有没有办法指定要导入的列的格式?另外,我不需要从 csv 文件导入所有列:我可以省略一些列吗?
\n首先,我编写了创建表的代码(抱歉,如果列名称是意大利语,但这并不重要):
\nCREATE TABLE IF NOT EXISTS bikes (\n bici INT,\n tipo_bici VARCHAR(20),\n cliente_anonimizzato INT,\n data_riferimento_prelievo DATE,\n data_prelievo TIMESTAMP,\n numero_stazione_prelievo INT,\n nome_stazione_prelievo TEXT,\n slot_prelievo SMALLINT,\n data_riferimento_restituzione DATE,\n data_restituzione TIMESTAMP,\n numero_stazione_restituzione INT,\n nome_stazione_restituzione TEXT,\n slot_restituzione SMALLINT,\n durata VARCHAR(10),\n distanza_totale REAL,\n co2_evitata REAL,\n calorie_consumate REAL,\n penalit\xc3\xa0 CHAR(2)\n);\n
Run Code Online (Sandbox Code Playgroud)\n然后我添加查询以将数据复制到表中:
\nCOPY bikes(\n bici,\n tipo_bici,\n cliente_anonimizzato,\n data_riferimento_prelievo,\n data_prelievo,\n numero_stazione_prelievo,\n nome_stazione_prelievo,\n slot_prelievo,\n data_riferimento_restituzione,\n data_restituzione,\n numero_stazione_restituzione,\n nome_stazione_restituzione,\n slot_restituzione,\n durata,\n distanza_totale,\n co2_evitata,\n calorie_consumate,\n penalit\xc3\xa0\n)\nFROM \'/Users/luca/tesi/data/2019q3.csv\'\nDELIMITER \',\'\nCSV HEADER;\n
Run Code Online (Sandbox Code Playgroud)\n代码看起来不错,除了弹出以下错误:
\nCREATE TABLE IF NOT EXISTS bikes (\n bici INT,\n tipo_bici VARCHAR(20),\n cliente_anonimizzato INT,\n data_riferimento_prelievo DATE,\n data_prelievo TIMESTAMP,\n numero_stazione_prelievo INT,\n nome_stazione_prelievo TEXT,\n slot_prelievo SMALLINT,\n data_riferimento_restituzione DATE,\n data_restituzione TIMESTAMP,\n numero_stazione_restituzione INT,\n nome_stazione_restituzione TEXT,\n slot_restituzione SMALLINT,\n durata VARCHAR(10),\n distanza_totale REAL,\n co2_evitata REAL,\n calorie_consumate REAL,\n penalit\xc3\xa0 CHAR(2)\n);\n
Run Code Online (Sandbox Code Playgroud)\n如何在CREATE TABLE
代码部分中指定要解析的格式?另外,我实际上并不需要这个 csv 的所有列,我该如何省略这些?我尝试仅指定我需要的内容,但出现导入错误:
COPY bikes(\n bici,\n tipo_bici,\n cliente_anonimizzato,\n data_riferimento_prelievo,\n data_prelievo,\n numero_stazione_prelievo,\n nome_stazione_prelievo,\n slot_prelievo,\n data_riferimento_restituzione,\n data_restituzione,\n numero_stazione_restituzione,\n nome_stazione_restituzione,\n slot_restituzione,\n durata,\n distanza_totale,\n co2_evitata,\n calorie_consumate,\n penalit\xc3\xa0\n)\nFROM \'/Users/luca/tesi/data/2019q3.csv\'\nDELIMITER \',\'\nCSV HEADER;\n
Run Code Online (Sandbox Code Playgroud)\n
设置datestyle
为ISO, DMY
,您的日期将根据您的需要进行解析。设置该参数没有任何问题 -SET
在您之前进行设置COPY
。
无法跳过 CSV 文件中的列。在表中添加额外的列并稍后删除它们,这很便宜。
归档时间: |
|
查看次数: |
10579 次 |
最近记录: |