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