Sub topic 2
Describing Your File: The CSV Format Options
This chapter assumes you know basic SQL, PostgreSQL data types, CSV files, and the psql command-line client. You will learn how to describe a CSV file accurately so PostgreSQL can parse it with COPY.
A CSV file is a sequence of fields
CSV means comma-separated values. Each line usually represents one record, and commas separate that record's fields.
For example:
id,name,price,notes
1,Keyboard,49.99,"Compact, wired"
2,Mouse,25.00,"Includes ""AA"" battery"The first line is a header. The first data row contains four fields. The comma inside Compact, wired does not split the field because the whole value is quoted. The doubled quotes in Includes ""AA"" battery represent one quote character in the stored value.
PostgreSQL's CSV parser needs to know the rules used by the file. The most important options are:
| Option | Purpose | Example |
|---|---|---|
| `FORMAT csv` | Selects CSV parsing rules | `FORMAT csv` |
| `HEADER true` | Skips the first record while importing | `HEADER true` |
| `DELIMITER` | Selects the field separator | `DELIMITER ';'` |
| `QUOTE` | Selects the character surrounding complex fields | `QUOTE '"'` |
| `ESCAPE` | Selects the character that escapes a quote | `ESCAPE '"'` |
| `NULL` | Defines text that means SQL `NULL` | `NULL 'NULL'` |
| `ENCODING` | Specifies the file character encoding | `ENCODING 'UTF8'` |
Describe the basic CSV structure
Use FORMAT csv in the WITH clause:
COPY products
FROM '/var/lib/postgresql/import/products.csv'
WITH (FORMAT csv);By default, CSV fields are separated by commas. The default quote and escape character is a double quote.
If the file has a header row, add HEADER true:
COPY products
FROM '/var/lib/postgresql/import/products.csv'
WITH (FORMAT csv, HEADER true);HEADER true tells PostgreSQL to skip the first CSV record. It does not compare the header names with the table's column names, and it does not rearrange columns. The file's fields still map by position unless you provide a column list.
Match nonstandard separators
Some systems produce semicolon-separated files. Tell PostgreSQL which character separates fields:
\copy products
FROM 'products.csv'
WITH (
FORMAT csv,
HEADER true,
DELIMITER ';'
);The delimiter must be a single character. It cannot also be the quote, escape, or newline character.
A delimiter inside a quoted field is treated as ordinary text. This allows a description such as "Keyboard; wired" to remain one field.
Understand quotes and escapes
A quoted field can contain the delimiter, a line break, or quote characters. To include the quote character itself, CSV normally writes it twice:
id,name,notes
1,Keyboard,"Compact, wired"
2,Mouse,"Includes ""AA"" battery"PostgreSQL uses " as both the default QUOTE and ESCAPE character. You can state the settings explicitly when documenting a file format:
\copy products
FROM 'products.csv'
WITH (
FORMAT csv,
HEADER true,
DELIMITER ',',
QUOTE '"',
ESCAPE '"'
);If a source system uses a different escape character, configure it to match the file. Do not change these settings casually. A mismatch can cause errors or shift later fields into the wrong columns.
Handle NULL and empty values
SQL NULL means “no value.” It is different from an empty string.
In CSV mode, an unquoted empty field normally represents NULL, while a quoted empty field represents an empty string:
id,name,notes
1,Keyboard,
2,Mouse,""The first row has a NULL notes value. The second row has a notes value of ''.
You can define another spelling for NULL:
\copy products
FROM 'products.csv'
WITH (
FORMAT csv,
HEADER true,
NULL 'NULL'
);With this setting, an unquoted field containing NULL becomes SQL NULL. A quoted "NULL" remains the literal text NULL.
Set the file encoding
Text files must use an encoding PostgreSQL can decode. UTF-8 is common:
\copy products
FROM 'products.csv'
WITH (
FORMAT csv,
HEADER true,
ENCODING 'UTF8'
);An encoding mismatch can produce invalid-character errors or corrupted text. Confirm the source system's encoding instead of guessing.
Exercise
A semicolon-separated file has a header, uses NULL for missing values, and contains commas inside quoted descriptions. Which options should you use?
Show answer
Use FORMAT csv, HEADER true, DELIMITER ';', NULL 'NULL'. Commas inside quoted fields need no additional option because the CSV parser protects quoted content.