Sub topic 6
NULLs, Empty Strings, and Quoting
This chapter assumes you know basic SELECT, CREATE TABLE, and PostgreSQL data types. You should also know how to use psql and understand that a CSV file stores one row per line with fields separated by a delimiter.
CSV has several ways to represent “no value” or text containing punctuation. PostgreSQL must distinguish these cases when it imports or exports data:
NULLmeans that a value is unknown or absent.- An empty string, written as
''in SQL, is a real text value with zero characters. - A quoted field is text whose delimiters are part of the CSV syntax, not part of the value.
The three common field values
Consider these CSV fields:
42,,,""The first field contains 42. The next two fields are unquoted and empty. The final field is a quoted empty string.
For a text column with PostgreSQL's default CSV settings, an unquoted empty field means NULL. A quoted empty field means an empty string. These values look similar in a text editor, but they have different meanings in the database.
| CSV field | PostgreSQL value | Meaning |
|---|---|---|
| `NULL` | The text `NULL` by default | Four ordinary characters |
| empty, with no characters | `NULL` by default | No value |
| `""` | Empty string | A text value containing zero characters |
| `"NULL"` | The text `NULL` | Quoting protects the characters |
The default null marker in CSV mode is an unquoted empty field. You can change it with the NULL option.
Choosing a null marker
Many exported files use the literal word NULL for missing values. Configure PostgreSQL to interpret that word as a database null:
\copy products (id, name, price, released, notes)
FROM 'products.csv'
WITH (
FORMAT csv,
HEADER true,
NULL 'NULL'
);With this configuration, a row such as the following has a null notes value:
7,"Keyboard",49.99,2026-01-15,NULLA quoted marker remains text:
7,"Keyboard",49.99,2026-01-15,"NULL"The notes value in the second row is the four-character string NULL, not a database null.
If an input file uses an empty field for an empty string, you cannot distinguish that value from a null using the default CSV settings. Use a separate marker, such as \N or NULL, for missing values and reserve quoted empty fields for empty strings.
Quoting delimiters and line breaks
A field must be quoted when its value contains the delimiter, a quote character, or a line break. For example, this row contains a comma in name and a comma and newline in notes:
8,"Mouse, wireless",19.95,2026-02-01,"Works with USB,
Bluetooth receivers"The quotes tell PostgreSQL that the comma in Mouse, wireless is part of the value. They also allow the notes value to span two physical lines.
In CSV mode, a quote inside a quoted field is represented by two consecutive quote characters:
9,"Cable ""Pro""",12.50,2026-02-02,"Includes a 2m cable"The imported name value is Cable "Pro".
PostgreSQL uses the double quote as both the default QUOTE and ESCAPE character in CSV mode. You can state these options explicitly when documenting a file format:
\copy products
FROM 'products.csv'
WITH (
FORMAT csv,
HEADER true,
DELIMITER ',',
QUOTE '"',
ESCAPE '"',
NULL 'NULL'
);The delimiter must not be confused with a character that can appear unquoted in ordinary text. If a file uses semicolons, set DELIMITER ';' consistently during both import and export.
Exporting without losing meaning
When PostgreSQL exports a null, it writes the configured null marker. When it exports an empty string, it quotes the field when necessary to keep it distinct from the null marker.
\copy (
SELECT id, name, notes
FROM products
ORDER BY id
)
TO 'products_export.csv'
WITH (
FORMAT csv,
HEADER true,
NULL 'NULL'
);Using the same delimiter, quote, escape, and null settings for import and export makes a round trip predictable.
Exercise
With NULL 'NULL', what PostgreSQL values result from the fields ,,"" in three text columns?
Show answer
The first field is an empty string because it is not the configured marker? No. An unquoted empty field is still empty and is not NULL when NULL is 'NULL', so the values are an empty string, an empty string, and an empty string. To avoid ambiguity, verify the exact behavior with a test table before relying on this format.