how to specify format of timestamp while creating table in postgreSQL?

…衆ロ難τιáo~ 提交于 2020-01-06 14:14:26

问题


I have created table with query:

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

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 '');

But I am getting following error:

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

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

Any ideas?

Thanks in advance.


回答1:


You can convert such a timestamp with:

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

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.



来源:https://stackoverflow.com/questions/21012128/how-to-specify-format-of-timestamp-while-creating-table-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!