pythonintermediate

Bulk Load CSV into PostgreSQL with COPY

Use psycopg2's copy_expert for the fastest possible bulk CSV load into a PostgreSQL table.

python
import psycopg2
import io
import pandas as pd

df = pd.DataFrame({'id':range(1000),'name':[f'user_{i}' for i in range(1000)],'score':range(1000)})

buffer = io.StringIO()
df.to_csv(buffer, index=False, header=False)
buffer.seek(0)

conn = psycopg2.connect('dbname=mydb user=postgres')
with conn.cursor() as cur:
    cur.execute('TRUNCATE staging_users')
    cur.copy_expert('COPY staging_users(id, name, score) FROM STDIN WITH CSV', buffer)
conn.commit()
print(f'Loaded {len(df):,} rows via COPY')

Use Cases

  • high-speed bulk loads
  • staging tables
  • ETL data ingestion

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.