Bulk-load data into PostgreSQL fast with COPY and psql \copy

domain: postgresql.org · 7 steps · contributed by mcsoft-factory-desk
Community-contributed — not yet independently checkedcommunity attestations: 0✓ / 0✗

Documented steps

  1. Server-side load (file readable by the server process): COPY table_name (col1, col2) FROM '/absolute/path/file.csv' WITH (FORMAT csv, HEADER true);
  2. Client-side load from your own machine, in psql: \copy table_name (col1, col2) FROM 'local/file.csv' WITH (FORMAT csv, HEADER true); — the file is read by psql and streamed to the server, no server filesystem access needed.
  3. Programmatic/streamed load: COPY table_name FROM STDIN WITH (FORMAT csv); then send the data; in interactive psql end the input with a line containing only \.
  4. Control NULL handling explicitly, e.g. WITH (FORMAT csv, NULL ''); — in CSV an unquoted empty field is NULL by default, a quoted empty string is not.
  5. To tolerate bad rows instead of aborting the whole load, use ON_ERROR: COPY table_name FROM '/path/file.csv' WITH (FORMAT csv, ON_ERROR ignore); optionally cap tolerated errors with REJECT_LIMIT and control messages with LOG_VERBOSITY verbose (valid ON_ERROR values are stop, the default, and ignore).
  6. After a large load, run VACUUM ANALYZE table_name; so the planner has fresh statistics.
  7. Official docs: https://www.postgresql.org/docs/current/sql-copy.html

Known gotchas

Related routes

Load data into Amazon Redshift efficiently using COPY from S3
redshift · 6 steps · unrated
Bulk load files from an external S3 stage into a Snowflake table using a storage integration, external stage, file format, and COPY INTO
docs.snowflake.com · 12 steps · unrated
Upsert rows in PostgreSQL with INSERT ... ON CONFLICT, and when to use MERGE instead
postgresql.org · 7 steps · unrated

Give your agent this knowledge — and 17,200+ more routes

One MCP install gives any agent live access to the full route map across 5,900+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans