[英]Importing .csv with timestamp column (dd.mm.yyyy hh.mm.ss) using psql \copy
I'm trying to import data from a .csv file into a postgresql 9.2 database using the psql \\COPY
command (not the SQL COPY). 我正在尝试使用
psql \\COPY
命令(而不是SQL COPY)将数据从.csv文件导入到postgresql 9.2数据库中。
The input .csv file contains a column with a timestamp in the dd.mm.yyyy hh.mm.ss format. 输入.csv文件包含一个时间戳为dd.mm.yyyy hh.mm.ss格式的列。
I've set the database datestyle to DMY using. 我使用了将数据库日期样式设置为DMY。
set datestyle 'ISO,DMY'
Unfortunately, when I run the \\COPY
command: 不幸的是,当我运行
\\COPY
命令时:
\COPY gc_test.trace(numpoint,easting,northing,altitude,numsats,pdop,timestamp_mes,duration,ttype,h_error,v_error)
FROM 'C:\data.csv' WITH DELIMITER ';' CSV HEADER ENCODING 'ISO 8859-1'
I get this error: 我收到此错误:
ERROR: date/time field value out of range: "16.11.2012 07:10:06"
错误:日期/时间字段值超出范围:“16.11.2012 07:10:06”
HINT: Perhaps you need a different "datestyle" setting.
提示:也许你需要一个不同的“日期风格”设置。
CONTEXT: COPY trace, line 2, column timestamp_mes: "16.11.2012 07:10:06"
语境:COPY trace,第2行,列timestamp_mes:“16.11.2012 07:10:06”
What is wrong with the datestyle? datestyle有什么问题?
Have you tried setting the datestyle
setting of the server ? 您是否尝试过设置服务器的日期
datestyle
设置 ?
SET datestyle = 'ISO,DMY';
You are using the psql meta-command \\copy
, which means the input file is local to the client. 您正在使用psql元命令
\\copy
,这意味着输入文件是客户端的本地文件。 But it's still the server who has to coerce the input to matching data-types. 但它仍然是必须强制输入匹配数据类型的服务器。
More generally, unlike the psql meta-command \\copy
which invokes COPY
on the server and is closely related to it .. I quote the manual concerning \\set
: 更一般地说,不像psql元命令
\\copy
在服务器上调用COPY
并且与它密切相关..我引用关于\\set
的手册 :
Note: This command is unrelated to the SQL command SET.
注意:此命令与SQL命令SET无关。
I found it difficult to apply 'SET datestyle' within the same session when applying the psql command. 我发现在应用psql命令时很难在同一个会话中应用'SET datestyle'。 Altering the datestyle on the whole database/server (just for the import) also might cause side effects on other users or existing applications.
更改整个数据库/服务器上的日期样式(仅用于导入)也可能会对其他用户或现有应用程序造成副作用。 So i usually modify the file itself before loading:
所以我通常在加载之前修改文件本身:
#!/bin/bash
#
# change from dd.mm.yyyy to yyyy-mm-dd inside the file
# note: regex searches for date columns separated by semicolon (;)
sed -i 's/;\([0-9][0-9]\)\.\([0-9][0-9]\)\.\([0-9][0-9][0-9][0-9]\);/;\3-\2-\1;/g' myfile
# then import file with date column
psql <connect_string> -c "\COPY mytable FROM 'myfile' ...."
The date style you seem to be using is German. 您似乎使用的日期样式是德语。 PostgreSQL supports this date style.
PostgreSQL支持这种日期风格。 Try using this:
试试这个:
SET datestyle TO German;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.