如何在postgreSQL中创建表时指定时间戳格式? [英] how to specify format of timestamp while creating table in postgreSQL?

查看:963
本文介绍了如何在postgreSQL中创建表时指定时间戳格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用查询创建了表:

CREATE TABLE Z (
A varchar(30),
B varchar(30),
C timestamp,
D numeric,
E varchar(30),
F varchar(30),
G varchar(30),
H numeric
);

现在,我有一个带定界符'|'的csv文件,该文件中的条目就像

Now, I have a csv file with delimiter '|' and entries in this file are like

1313131|131313131|20130103115041|20.0000000|ABCD|EFGH||0.0000000

我正在尝试使用以下命令将表中的数据导入:

I am trying following command to import the data in the table:

\copy Z from filename WITH (FORMAT CSV , DELIMITER '|', NULL '');

但是我遇到以下错误:

ERROR:  invalid input syntax for type timestamp: "20130103115041"
CONTEXT:  COPY Z, line 1, column C: "20130103115041"

我想我必须在创建表时指定时间戳格式,以便它可以接受 YYYYMMDDHHMMSS格式的输入。

I guess I have to specify the timestamp format while creating table so that it can accept the input in "YYYYMMDDHHMMSS" format.

有什么想法吗?

预先感谢。

推荐答案

您可以将这样的时间戳转换为:

You can convert such a timestamp with:

SELECT to_timestamp('20130103115041','YYYYMMDDHHMISS');

但是, COPY 不提供输入过滤器。因此,您可能需要 CREATE UNLOGGED TABLE ... 来存储 COPY 数据,然后执行 INSERT INTO ... SELECT ... 插入并转换数据。

However, COPY doesn't offer input filters. So you'll probably want to CREATE UNLOGGED TABLE ... to store the COPY data, then do an INSERT INTO ... SELECT ... to insert and transform the data.

这篇关于如何在postgreSQL中创建表时指定时间戳格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆