Python psycopg2 copy_from () to load data throws error for null integer values: DataError: invalid input syntax for integer: ""
I am trying to load data from a python StringIO object into a Postgres database table using the psycopg2 copy_from () method.
My copy_from instance does not perform the first write directly on a specific null column that has a null value ("no quotes"). I also tried to use the Python keyword None instead of '' for NULL values. This throws me an error: DataError: Invalid input syntax for integer: "" CONTEXT: COPY, line 1, column: "
The code looks something like this:
table_data = StringIO.StringIO()
# Populate the table_data variable with rows delimited by \n and columns delimited by \t
cursor = db_connection.cursor()
cursor.copy_from(table_data, <table_name>)
This column is a small column.
source to share
By default COPY FROM
(and copy_from
) encodes the NULL value as \N
. If you want to use an empty string to mean NULL, you need to say it explicitly :
cursor.copy_from(table_data, table_name, null="")
source to share