简体   繁体   English

使用psql \\ copy导入带有时间戳列(.dd.mm.yyyy hh.mm.ss)的.csv

[英]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.

相关问题 如何在 Postgres 中将 YYYY-MM-DDTHH:mm:ss.SSSZ 时间戳转换为 YYYY-MM-DD HH:mm:ss - How to convert YYYY-MM-DDTHH:mm:ss.SSSZ timestamp to YYYY-MM-DD HH:mm:ss in Postgres 将秒转换为 yyyy-mm-dd hh:mm:ss PostgreSQL - Convert seconds to yyyy-mm-dd hh:mm:ss PostgreSQL Django DateTimeField 时间戳值的格式无效。 它必须是 YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] 格式 - Django DateTimeField Timestamp value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format 如何转换为日期时间YYYY-MM-DD HH:mi:ss - How to Convert to Datetime YYYY-MM-DD HH:mi:ss 将任何输入时间戳 ex (4/25/2021 20.01.42) 转换为正确的 Postgres 时间戳 (YYYY-MM-DD HH12:MI:SS) - Convert any input timestamp ex (4/25/2021 20.01.42) to Correct Postgres TimeStamp (YYYY-MM-DD HH12:MI:SS) 在Postgresql中存储格式为yyyy-mm-dd hh:mm:ss的日期的数据类型 - Datatype to store date of format yyyy-mm-dd hh:mm:ss in postgresql 以oracle和PostgreSQL查询以yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSSZ格式打印日期 - Query to print date in yyyy-MM-dd'T'HH:mm:ss.SSSZ format in oracle and postgresql 表中的日期为dd.mm.yyyy-无法通过csv导入到postgres - Date in table is dd.mm.yyyy - Can't import to postgres via csv Spring - LocalDateTime - 如何只返回小时和分钟而不是 dd-mm-yyyy hh-MM-ss-sss - Spring - LocalDateTime - how to return just the hours and minutes instead of dd-mm-yyyy hh-MM-ss-sss Sequelize 如何将日期格式化为 YYYY-MM-DD HH:mm:ss 并将列保持为蛇形 - Sequelize How do you format date to YYYY-MM-DD HH:mm:ss and keep the columns as snake case
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM