
Importing Only Some CSV Columns Into a PostgreSQL Table
COPY cannot skip file columns. Compare two ways to load three of eight columns of a 2,000,000 row CSV, with timings, and see why cut breaks on quotes.
Everything you need to level up your coding skills.

COPY cannot skip file columns. Compare two ways to load three of eight columns of a 2,000,000 row CSV, with timings, and see why cut breaks on quotes.

COPY stops with an invalid byte sequence error when a CSV is Latin 1. Fix it with the ENCODING option or client_encoding, and avoid the SQL_ASCII trap.

The COPY error means a row has more fields than the table has columns. See the four usual causes, how to find the line, and the fix for each one.

Skip the header, or better, check it. See what HEADER true, HEADER match and a column list do, and the errors when the file and the table disagree.

The two commands differ in which machine reads the file. See the permission errors, the path errors and the timings, and choose the one that fits.

The COPY command in practice: the options that matter, the two ways to feed it, what its row count means, and why one bad value undoes the whole load.

Load a CSV into PostgreSQL with COPY, see what makes it fast, and learn what each failure means, with a script that loads in one transaction.

Find the lines and bytes that break a UTF-8 load with iconv, isutf8, grep or a short Python script, and see how PostgreSQL and DuckDB report them.

MySQL utf8 columns hold three bytes per character, so an emoji becomes a question mark during a CSV load with only a warning. See the fix and the checks.

Question marks in a CSV are lost data, not a display bug. See where the substitution happens, count the damage and decide whether the export is broken.

A NUL byte can cut a PostgreSQL load short while Python keeps it. Find them with grep, strip them from 130 MB in under a second, and keep tabs and newlines.

Excel on Windows often shows UTF-8 accents as à symbols. Add a BOM when you write the file, or import it with the right encoding, and check the bytes first.
We send one email when a new article is published. No spam.