C. CHEN
C. CHEN

Reputation: 21

How to skip first several lines when importing CSV from Postgresql?

I was trying to import a CSV file to my Postgresql with the frist 8 lines skipped and start from the 9th line. My codes below works to read from the second line and treat the first line as header:

    create table report(
            id integer,
            name character(3),
            orders integer,
            shipments float

            );

COPY report 
FROM 'C:\Users\sample.csv' DELIMITER ',' CSV HEADER;

Now how to improve this code to read from the 9th line. Thank you!

CSV details

Upvotes: 1

Views: 3184

Answers (1)

Daniel Vérité
Daniel Vérité

Reputation: 61726

With PostgreSQL 9.3 or newer, COPY can refer to a program to preprocess the data, for instance Unix tail.

To start an import at line 9:

COPY report  FROM PROGRAM 'tail -n +9 /path/to/file.csv' delimiter ',' csv;

Apparently you're using Windows, so tail might not be immediately available. Personally I would install it from MSYS, otherwise there are alternatives mentioned in
Looking for a windows equivalent of the unix tail command
or Windows equivalent of the 'tail' command.

Upvotes: 2

Related Questions