Big thanks in advance, relatively new to psycopg2.
I'm trying to bulk insert data in the form of a pandas dataframe to my existing postgres database.
try:
psycopg2.extras.execute_values(
cur=cur,
sql=sql.SQL("""
INSERT into {table_name} ( {columns} )
VALUES %s
""").format(table_name=Identifier(entity),
columns=SQL(', ').join(map(Identifier, column_names))
),
argslist=dataframe,
template=None,
page_size=500)
except Exception as error:
print(ERROR: ' + error)
I get the error below when I run this:
string index out of range
I tried changing the dataframe to a dict, using:
dataframe = dataframe.to_dict(orient='records')
The output that I am getting from the except clause is now as follows:
'dict' object does not support indexing
Any help hugely appreciated, I'm not sure what the issue is here.
Thanks in advance
(%(dict_key_1)s, %(dict_key_2)s, ...)instead of%s