Skip to content
Advertisement

insert datetime from csv to postgres error

I have a csv file:

output:

And then I tried to insert the file data into postgres database:

Error:

It works in psql client.

I thought it is the string character ‘T02’ issue,so I use:

Then the error became:

Then I thought I need convert it to datetime first:

Same error as the last one.

Then I did some research:

Error:

I have stuck here for 2 days,any friend can help?

Advertisement

Answer

UPDATE: Comparing the working psql client input against the Line 3 Error, you are missing single quotes around the individual values in the cur.execute() statement.

Original Answer: (This focused on the .Replace(‘T’) issue later in the question)

2021-07-07T02:27:00Z is in ISO 8601 UTC format. It includes a date, time, and a timezone offset. Apparently, your table’s event_time column is timestamp format with date and time only.

You will need to convert your incoming date with time zone to your local timezone format to omit the timezone.

It looks like you were on the right track casting the data to a ::timestamp, but it won’t discard the timezone information, it expects you to do a proper conversion.

I believe this StackOverflow answer explains the conversions well: PostgreSQL wrong converting from timestamp without time zone to timestamp with time zone

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement