I have a file that I want to copy into a table, the table has three columns,
create table item (
name Text,
t Time without time zone,
status Text);
the File looks like this:
book, 2022-08-13 13:30:01,out
char,2022-08-13 13:35:22, inside
lather,2022-08-13 13:36:00, out
is there a way to convert the Datetime value to time value without do a table with Datetime and then Altering it?
2
Answers
The best possible way that I think would be to create a staging table with a timestamp column to temporarily hold the data. Use the
COPY
command to load the CSV data into the staging table. Then, insert the data into your target table, converting the datetime values to time usingdatetime::time
in the SELECT statement. Also you can drop the staging table if it’s no longer needed.Create a staging table to hold the raw data:
Copy the data from the file into the staging table:
Insert the transformed data into your target table:
The CSV file:
Create the table:
Import the data using
psql
:The result:
The datetime string will automatically be cast to time on import.