Missed in PostgreSQL 19 : Support multi-line headers in COPY FROM command.

On 3rd of July 2025, Fujii Masao committed patch:

Support multi-line headers in COPY FROM command.
 
The COPY FROM command now accepts a non-negative integer for the HEADER option,
allowing multiple header lines to be skipped. This is useful when the input
contains multi-line headers that should be ignored during data import.
 
Author: Shinya Kato <shinya11.kato@gmail.com>
Co-authored-by: Fujii Masao <masao.fujii@gmail.com>
Reviewed-by: Yugo Nagata <nagata@sraoss.co.jp>
Discussion: https://postgr.es/m/CAOzEurRPxfzbxqeOPF_AGnAUOYf=Wk0we+1LQomPNUNtyZGBZw@mail.gmail.com

PostgreSQL 19 is out, and while going through release notes I noticed that there are some things that I didn't write about. So, let's fix it, starting with this small, but very practical change.

If you ever got CSV file from some reporting system, you know that quite often it doesn't start with simple header. Instead, you get some kind of title, information about who/when generated it, and only then column names.

For example, like this file:

Monthly sales export
Generated: 2026-11-24 06:00, by: reporting-system v2.3
region,product,units,amount
north,widget,10,99.90
south,gadget,3,45.00
east,widget,7,69.93

So far, COPY could skip only single header line. Which, in this case, isn't enough:

=$ create table sales (region text, product text, units int4, amount numeric);
CREATE TABLE
 
=$ \copy sales from sales.csv with (format csv, header true)
ERROR:  missing data for column "units"
CONTEXT:  COPY sales, line 2: "Generated: 2026-11-24 06:00, by: reporting-system v2.3"

Normally I would have to pre-process the file with tail, sed, or something like this. But now, HEADER option can be given a number, which is number of lines to skip:

=$ \copy sales from sales.csv with (format csv, header 3)
COPY 3
 
=$ select * from sales;
 region │ product │ units │ amount
────────┼─────────┼───────┼────────
 north  │ widget  │    10 │  99.90
 south  │ gadget  │     3 │  45.00
 east   │ widget  │     7 │  69.93
(3 rows)

Nice. It works also for text format, not only csv:

=$ truncate sales;
TRUNCATE TABLE
 
=$ copy sales from stdin with (header 2);
junk line
another junk line
north,widget,10,99.90
\.
COPY 1

Of course header 1 works just like header true did, and header 0 is the same as header false.

There are some limitations, though. You can't use it for COPY TO, which is pretty obvious – PostgreSQL wouldn't know what to put in these additional lines:

=$ copy sales to stdout with (format csv, header 2);
ERROR:  cannot use multi-line header in COPY TO

And one thing that you should be aware of – if the number is larger than number of lines in the input, you don't get any error. Just nothing gets loaded:

=$ copy sales from stdin with (format csv, header 5);
region,product,units,amount
south,gadget,3,45.00
\.
COPY 0

Also, there was a follow-up commit, so the same thing works for file_fdw, so you can define foreign table on top of such file, with header ‘3' option.

Small thing, but definitely something that will save me some sed calls. Thanks to everyone involved.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.