问题
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