'Insert values into table from csv file using psycopg2

I try to insert values into table from my csv file. I write this code:

conn = ps.connect(
                        host="#######",
                        database="#####",
                        user="######",
                        port="#####",
                        password="######")

cur = conn.cursor()

with open('C:\\Users\\maciej.olech\\Desktop\\structure_files\\MAG.csv', 'r') as f:
     
    next(f)
    cur.copy_from(f, 'mag', sep=',')

conn.commit()

This work fine when I insert files that dont have empty spaces. When I try to load a file which contains empty fields i get this error:

InvalidDatetimeFormat: invalid input syntax for type date: "" CONTEXT: COPY mag, line 456193, column kdur: ""

Any ideas how to solve it? Thanks for your help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source