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
    First dump the excel sheet to CSV (and pray for the right quoting and locale settings). Next, import the resulting .csv file. Commented Feb 17, 2013 at 19:13
  • i just did what u said but i am facing 2 issues: a) my customer id , cid, is an int in postgesql but when i copy from my .csv file i get 'invalid input syntax for integer cid'. does that mean i have to change my datatype to a varchar in my postgresql table? or is there a way to change that in the .csv file b) My credit_card_number attribute is 16 characters but i am getting an error message: value too long for type character varying(16) can u plz help me with this? Commented Feb 17, 2013 at 19:16
  • Have you done a SET client_encoding = win1252; before the \copy ... line? (not sure about the exact syntax, please check the Fine Manual for that) UPDATE: you don't need varchar(16), just use varchar, and the size will be virtually unlimited. Not sure about the cid. More info is needed. Commented Feb 17, 2013 at 19:20
  • You could be having an issue with PostgreSQL's cid (look near the bottom of the page), try renaming your cid to something else. If you're having a length problem with a varchar(16) then you have something longer than 16 characters, consider cleaning up your data before trying to import it. Commented Feb 17, 2013 at 19:42

1 Answer 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.