How to recognize fieldnames in header row of text/csv file when importing in postgreSQL table ?

StackOverflow https://stackoverflow.com/questions/14911561

문제

I want to import a fair number of csv files in a single postgreSQL table. The csv files differ in the number of fields they contain, but the header row contains fieldnames which correspond with those in the destination table.

Rather than specifying the fields present in each csv file using:

COPY destination_table (comma_separated_fields_in_source_file) FROM '/filepath'...

I was wondering whether there is a way to have postgres / pgAdmin recognise the fieldnames in the header row?

Thanks.

올바른 솔루션이 없습니다

다른 팁

this is naughty, and untested, but might work:

bash -c 'echo copy tablename \(; head -1 filename; echo \) from stdin with format csv header\;; cat filename ' | psql

COPY does not support this natively, you might, however, think of writing a function/stored procedure to handle this. Maybe using a 'Foreign Data Wrapper' to read the first line and dynamically create a 'COPY' SQL statement from this. See here for the text/csv Foreign Data Wrapper: http://www.postgresql.org/docs/current/static/file-fdw.html

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top