Sub topic 7
Loading into a Subset or Different Order of Columns
This chapter assumes you know basic SQL, PostgreSQL data types, CSV files, and the psql command-line client. It also assumes the products table from earlier chapters exists:
CREATE TABLE products (
id integer PRIMARY KEY,
name text NOT NULL,
price numeric(10,2),
released date,
notes text
);Why the Column List Matters
A CSV file contains fields in a fixed order. PostgreSQL maps those fields to table columns in the order that you provide. The CSV header is not used automatically to find matching column names.
Without a column list, PostgreSQL maps the first field to the first table column, the second field to the second table column, and so on:
COPY products
FROM '/var/lib/postgresql/import/products.csv'
WITH (FORMAT csv, HEADER true);This expects fields in this order:
| CSV field | Table column |
|---|---|
| 1 | `id` |
| 2 | `name` |
| 3 | `price` |
| 4 | `released` |
| 5 | `notes` |
A column list makes the mapping explicit:
\copy products (id, name, price, released, notes)
FROM 'products.csv'
WITH (FORMAT csv, HEADER true)The list after products describes the destination columns. The fields in each CSV row must appear in that same order.
Loading a Different Order
Suppose the file stores the product name first, followed by the ID and price:
name,id,price
Keyboard,101,49.99
Mouse,102,19.50The destination table still expects id, name, and price, but the file has a different order. Match the file order in the column list:
\copy products (name, id, price)
FROM 'products.csv'
WITH (FORMAT csv, HEADER true)The first field goes to name, the second goes to id, and the third goes to price. PostgreSQL does not care that the table definition lists id first.
The column list can contain any table columns, as long as each name appears only once and the incoming values can be converted to the target types.
Loading a Subset of Columns
A file may contain only some of the table's columns. For example:
id,name,price
103,Monitor,249.00Load it by naming only those three destination columns:
\copy products (id, name, price)
FROM 'products.csv'
WITH (FORMAT csv, HEADER true)The omitted columns, released and notes, receive their normal defaults. If a column has no default, PostgreSQL inserts NULL, provided the column allows NULL.
For example, this table supplies a default value for a missing column:
CREATE TABLE inventory (
sku integer PRIMARY KEY,
name text NOT NULL,
quantity integer NOT NULL DEFAULT 0,
comment text
);
\copy inventory (sku, name)
FROM 'inventory.csv'
WITH (FORMAT csv, HEADER true)A row containing only sku and name receives 0 for quantity and NULL for comment.
Common Mapping Errors
These mistakes cause many failed or incorrect imports:
- Leaving out the column list when the file order differs from the table order.
- Listing columns in table order instead of file order.
- Supplying fewer fields than the listed destination columns.
- Trying to load a CSV header as data by omitting
HEADER true. - Assuming a header such as
priceautomatically maps to thepricecolumn. - Sending a value that cannot be converted, such as
unknowninto anintegercolumn.
The column list controls position, not names in the CSV header. Keep HEADER true when the first line contains labels.
A Practical Pattern
Use a column list whenever a file is produced by another system, contains a subset of fields, or may change order. It documents the contract between the file and the table:
\copy products (name, id, released, price)
FROM 'products.csv'
WITH (
FORMAT csv,
HEADER true,
NULL 'NULL'
)Here, the file must provide four fields in this order: name, ID, release date, and price. The notes column is omitted and receives its default or NULL.
Exercise
A CSV file contains sku,description in that order, but the table columns are description text, sku integer, active boolean NOT NULL DEFAULT true. Which COPY command loads the file and preserves the default for active?
Show answer
\copy items (sku, description) FROM 'items.csv' WITH (FORMAT csv, HEADER true). The destination column list follows the file order, and the omitted active column receives its default.