PostgreSQL COPY — Fast CSV Import
Use PostgreSQL COPY command for high-speed bulk data loading from CSV files with error handling.
-- Basic COPY FROM (server-side, file on DB server)
COPY orders (id, customer_id, amount, status, created_at)
FROM '/data/orders.csv'
WITH (
FORMAT csv,
HEADER true,
DELIMITER ',',
NULL '',
QUOTE '"',
ESCAPE '"'
);
-- Client-side copy (\copy from psql — file on client machine)
-- \copy orders FROM 'local_file.csv' CSV HEADER
-- COPY to staging table, then merge
CREATE TEMP TABLE staging_orders (LIKE orders INCLUDING DEFAULTS);
COPY staging_orders FROM '/data/orders.csv' CSV HEADER;
-- Upsert from staging
INSERT INTO orders
SELECT * FROM staging_orders
ON CONFLICT (id) DO UPDATE SET
amount = EXCLUDED.amount,
status = EXCLUDED.status,
updated_at = NOW();
-- Export: COPY TO
COPY (
SELECT id, email, created_at
FROM users
WHERE created_at >= '2024-01-01'
)
TO '/tmp/users_export.csv'
WITH (FORMAT csv, HEADER true);
-- Check row count after import
SELECT COUNT(*) AS imported_rows FROM orders;
-- Performance: COPY is typically 5-10x faster than INSERT
-- For very large loads, consider:
-- 1. Drop indexes before COPY, rebuild after
-- 2. Increase maintenance_work_mem
-- 3. Use UNLOGGED tables for stagingSponsored
Supabase
Use Cases
- High-speed bulk data loading into PostgreSQL
- ETL pipeline data ingestion step
- Migrating CSV data to production databases
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
Read Large CSV in Chunks with Pandas
Process CSV files larger than RAM by reading in chunks — memory-efficient ETL pattern for data pipelines.
Best for: Processing multi-GB CSV files without running out of memory
Database Backup and Restore to S3
Automated PostgreSQL backup script with compression, S3 upload, retention policy, and restore commands.
Best for: Automated daily database backups to S3
Bulk Load CSV into PostgreSQL with COPY
Use psycopg2's copy_expert for the fastest possible bulk CSV load into a PostgreSQL table.
Best for: high-speed bulk loads
Python ETL Pipeline Example
Complete extract-transform-load pipeline with error handling, logging, and incremental processing.
Best for: Automating data ingestion from CSV to warehouse