Can I automatically create a table in PostgreSQL from a csv file with headers?
csv, postgresql
Solution
You can't find anything in the `COPY` documentation, because COPY cannot create a table for you. You need to do that before you can `COPY` to it.
Problem
I'm running PostgreSQL 9.2.6 on OS X 10.6.8. I would like to import data from a CSV file with column headers into a database. I can do this with the `COPY` statement, but only if I first manually create a table with a column for each column in the CSV file. Is there any way to automatically create this table based on the headers in the CSV file? Per this question I have tried `COPY test FROM '/path/to/test.csv' CSV HEADER;` But I just get this error: `ERROR: relation "test" does not exist` And if I first create a table with no columns: `CREATE TABLE test ();` I get: `ERROR: extra data after last expected column` I can't find anything in the PostgreSQL COPY documentation about automatically creating a table. Is there some other way to automatically create a table from a CSV file with headers?