1

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

C. CHEN
  • 21
  • 1
  • 3

1 Answers1

2

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.

Community
  • 1
  • 1
Daniel Vérité
  • 58,074
  • 15
  • 129
  • 156
  • No need for a msys or similar things. The command line programs can be downloaded as native windows binaries: http://unxutils.sourceforge.net/ or http://gnuwin32.sourceforge.net/ –  Apr 19 '16 at 20:40