PostgreSQL's COPY
command and the psql
\copy
wrapper for it do not understand or support the Microsoft Office Excel (xls
) or Microsoft Office XML Spreadsheet (xlsx
) file formats.
You must either save the Excel file as CSV and use \copy ... CSV
, or use an ETL tool that does understand the Microsoft Excel format. Saving as CSV will be the easiest way by far.
The data in the Excel sheet must be compatible with the PostgreSQL column definitions for the table you are copy
ing into. You cannot copy values like ABC123
into an integer
column, for example.
If your Excel data is messy, full of invalid values, and otherwise problematic consider cleaning it up in Excel first by adding validation. Alternately you could import it into a TEMPORARY
or UNLOGGED
PostgreSQL table where the problem columns are all defined with the text
data-type, then use an INSERT INTO ... SELECT
command to insert cleaned up data into the final table. A final option is again to use an ETL tool like one of the aforementined to clean the data up as it's loaded and inserted.
Which approach you choose will depend on whether you're more comfortable working with SQL queries, with ETL tools, or with Excel.