Search code examples
postgresqlpsqlpostgresql-copy

Is it possible to make the psql \copy see a line inside a csv file as a comment?


I'm successfully inserting csv files to postgresql with the following command:

\COPY tablename(col1, col2, col3) FROM '/home/user/mycsv.txt' WITH CSV HEADER DELIMITER ';' NULL AS 'null';

However, I'd like to write some metadata inside this csv file with data that's repeatable. I know I could create a different file to store this data but I think it'd be a lot more convenient to store this metadata in the same csv file where the majority of the data is stored. I imagine a file like the following:

-- commented line with some metadata
col1;col2;col3
value;value;value
value;value;value
value;value;value

I've tried using -- /* /* and # as comments but the \copy command fails to import the data when I do that. Is there any way that I can tell the \copy psql command to see specific lines as comments just so I can insert data with lines that are not a part of the csv file? Is it possible?


Solution

  • Use the FROM PROGRAM construct to tell something else to filter them out.

    \COPY tablename(col1, col2, col3) FROM PROGRAM 'egrep -v "^-- " mycsv.txt' WITH CSV HEADER DELIMITER ';' NULL AS 'null'