Search code examples
postgresqlcsvheadercopyfooter

PostgreSQL COPY additional header - footer rows in csv


I want to COPY a couple of csv-Files to my database. However they have additional header / footer rows containing information, which is not in the csv format. In example:

Version: xxxxxxxxx
Start
Column1;Column2;Column3
1;2;3
4;5;6
7;8;9
End
Rows of Data: 3

Is there a way of skipping the additional rows / columns?


Solution

  • If the file structure is always the same - in your case first two and last two invalid - you can use FROM PROGRAM and fix the file with head and tail on the fly:

    \COPY t FROM PROGRAM 'tail -n +3 file.csv | head -n -2' CSV DELIMITER ';' HEADER;