0

I want to populate my Customer database from an excel file and i am using the \copy command but i get the following error. can anyone please help me with this?

\copy customer from '/home/2008/uehtes/Desktop/Comp421/data.xlsx';
ERROR:  invalid byte sequence for encoding "UTF8": 0xde76
CONTEXT:  COPY customer, line 1
4

1 に答える 1

4

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 copying 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.

于 2013-02-17T23:07:08.260 に答える