Last active
January 10, 2023 19:03
-
-
Save ellisvalentiner/63b083180afe54f17f16843dd51f4394 to your computer and use it in GitHub Desktop.
Recipe for (fast) bulk insert from python Pandas DataFrame to Postgres database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env/python | |
import psycopg2 | |
import os | |
from io import StringIO | |
import pandas as pd | |
# Get a database connection | |
dsn = os.environ.get('DB_DSN') # Use ENV vars: keep it secret, keep it safe | |
conn = psycopg2.connect(dsn) | |
# Do something to create your dataframe here... | |
df = pd.read_csv("file.csv") | |
# Initialize a string buffer | |
sio = StringIO() | |
sio.write(df.to_csv(index=None, header=None)) # Write the Pandas DataFrame as a csv to the buffer | |
sio.seek(0) # Be sure to reset the position to the start of the stream | |
# Copy the string buffer to the database, as if it were an actual file | |
with conn.cursor() as c: | |
c.copy_from(sio, "schema.table", columns=df.columns, sep=',') | |
conn.commit() |
I can't get it to work :( I think it's because in the dataframe everything is string but in the table there's a mixture of data types
This is the error I'm getting at the moment:
c.copy_from(sio, "public.sku", columns=df_ora.columns, sep=',')
psycopg2.errors.InvalidTextRepresentation: invalid input syntax for type numeric: ""
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@rehoter-cyber could you try using the
pandas.DataFrame.to_sql
method but use the parametermethod=psql_insert_copy
, wherepsql_insert_copy
is the callable function defined in the Insertion method documentation