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?
It depends on what type of machine/encoding generated your import file.
If you're getting it from an English or Western European version of Windows, your best bet is probably setting it to 'WIN1252'. If you are getting it from a different source, consult the list of character encodings here:
http://www.postgresql.org/docs/8.3/static/multibyte.html
If you're getting it from a Mac, you may have to run it through the "iconv" utility first to convert it from MacRoman to UTF-8.
It is also very possible with this error that the field is encrypted in place. Be sure you are looking at the right table, in some cases administrators will create an unencrypted view that you can use instead. I recently encountered a very similar issue.
Adding
encoding
option worked in my case.Open file CSV by Notepad++ . Choose menu
Encoding
\Encoding in UTF-8
, then fix few cell manuallly.Then try import again.
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.(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:
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.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".
Well I was facing the same problem. And what solved my problem is this:
In excel click on Save as. From save as type, choose .csv Click on Tools. Then choose web options from drop down list. Under Encoding tab, save the document as Unicode(UTF-8). Click OK. Save the file. DONE !