Search code examples
postgresqlimport

invalid byte sequence for encoding "UTF8"


I'm trying to import some data into my database. So I've created a temporary table,

create temporary table tmp(pc varchar(10), lat decimal(18,12), lon decimal(18,12), city varchar(100), prov varchar(2));

And now I'm trying to import the data,

 copy tmp from '/home/mark/Desktop/Canada.csv' delimiter ',' csv

But then I get the error,

ERROR:  invalid byte sequence for encoding "UTF8": 0xc92c

How do I fix that? Do I need to change the encoding of my entire database (if so, how?) or can I change just the encoding of my tmp table? Or should I attempt to change the encoding of the file?


Solution

  • If you need to store UTF8 data in your database, you need a database that accepts UTF8. You can check the encoding of your database in pgAdmin. Just right-click the database, and select "Properties".

    But that error seems to be telling you there's some invalid UTF8 data in your source file. That means that the copy utility has detected or guessed that you're feeding it a UTF8 file.

    If you're running under some variant of Unix, you can check the encoding (more or less) with the file utility.

    $ file yourfilename
    yourfilename: UTF-8 Unicode English text
    

    (I think that will work on Macs in the terminal, too.) Not sure how to do that under Windows.

    If you use that same utility on a file that came from Windows systems (that is, a file that's not encoded in UTF8), it will probably show something like this:

    $ file yourfilename
    yourfilename: ASCII text, with CRLF line terminators
    

    If things stay weird, you might try to convert your input data to a known encoding, to change your client's encoding, or both. (We're really stretching the limits of my knowledge about encodings.)

    You can use the iconv utility to change encoding of the input data.

    iconv -f original_charset -t utf-8 originalfile > newfile
    

    You can change psql (the client) encoding following the instructions on Character Set Support. On that page, search for the phrase "To enable automatic character set conversion".