将 csv 文件复制到 PostgreSQL 表时解析 DATE

bag*_*nte 7 postgresql import datetime date csv

我有一长串.csv文件,我想将其导入本地数据库。DATE我相信我的查询是正确的,但解析和列存在一些问题TIMESTAMP。PostgreSQL 读取这些列时需要 ISO 格式“yyyy/mm/dd”,但我的数据采用另一种格式:“dd/mm/yyyy”。

\n

我在网上和其他 Stack Overflow 答案上读到,可以有所SET不同datestyle,但不建议这样做。

\n

有没有办法指定要导入的列的格式?另外,我不需要从 csv 文件导入所有列:我可以省略一些列吗?

\n

细节

\n

首先,我编写了创建表的代码(抱歉,如果列名称是意大利语,但这并不重要):

\n
CREATE 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

然后我添加查询以将数据复制到表中:

\n
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

代码看起来不错,除了弹出以下错误:

\n
CREATE 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 的所有列,我该如何省略这些?我尝试仅指定我需要的内容,但出现导入错误:

\n
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

Lau*_*lbe 4

设置datestyleISO, DMY,您的日期将根据您的需要进行解析。设置该参数没有任何问题 -SET在您之前进行设置COPY

无法跳过 CSV 文件中的列。在表中添加额外的列并稍后删除它们,这很便宜。

  • 嗨劳伦兹,感谢您的帮助!一切顺利。我添加的行是“SET datestyle TO iso, dmy;”,这很好。作为参考,我查看了“SET”的[官方文档](https://www.postgresql.org/docs/current/sql-set.html#examples)。 (2认同)