ERROR: invalid byte sequence for encoding "UTF8": 0xff - postgresql

Im importing a databse called adventure works to postgresql
and these message appears
ERROR: invalid byte sequence for encoding "UTF8": 0xff
CONTEXT: COPY businessentity, line 1
SQL state: 22021

As the error says, the byte 0xFF isn't valid in a UTF8 file. Since you're trying to load data from a SQL Server sample database I suspect the file was saved as UTF16 with a Byte Order Mark. Unicode isn't a single encoding. Unicode text files can contain a signature at the start which specifies the encoding used in the file. As the link shows, for UTF16 the BOM can be 0xFF 0xFE or 0xFE 0xFF, values which are invalid in UTF8.
As far as I know you can't specify a UTF16 encoding with COPY, so you'll have to either convert the CSV file to UTF8 with a command line tool or export it again as UTF8. If you exported the data using any SQL Server tool (SSMS, SSIS, bcp) you can easily specify the encoding you want. For example :
bcp Person.BusinessEntity out "c:\MyPath\BusinessEntity.csv" -c -C 65001
Will export the data using the 65001 codepage, which is UTF8

Related

Character with byte sequence 0x9d in encoding 'WIN1252' has no equivalent in encoding 'UTF8'

I am reading a csv file in my sql script and copying its data into a postgre sql table. The line of code is below :
\copy participants_2013 from 'C:/Users/Acrotrend/Desktop/mip_sahil/mip/reelportdata/Participating_Individual_Extract_Report_MIPJunior_2013_160414135957.Csv' with CSV delimiter ',' quote '"' HEADER;
I am getting following error : character with byte sequence 0x9d in encoding 'WIN1252' has no equivalent in encoding 'UTF8'.
Can anyone help me with what the cause of this issue and how can I resolve it?
The problem is that 0x9D is not a valid byte value in WIN1252.
There's a table here: https://en.wikipedia.org/wiki/Windows-1252
The problem may be that you are importing a UTF-8 file and postgresql is defaulting to Windows-1252 (which I believe is the default on many windows systems).
You need to change the character set on your windows command line before running the script with chcp. Or in postgresql you can:
SET CLIENT_ENCODING TO 'utf8';
Before importing the file.
Simply specify encoding 'UTF-8' as the encoding in the \copy command, e.g. (I broke it into two lines for readability but keep it all on the same line):
\copy dest_table from 'C:/src-data.csv'
(format csv, header true, delimiter ',', encoding 'UTF8');
More details:
The problem is that the Client Encoding is set to WIN1252, most likely because it is running on Windows machine but the file has a UTF-8 character in it.
You can check the Client Encoding with
SHOW client_encoding;
client_encoding
-----------------
WIN1252
Any encoding has numeric ranges of valid code. Are you sure so your data are in win1252 encoding?
Postgres is very strict and doesn't import any possible encoding broken files. You can use iconv that can works in tolerant mode, and it can remove broken chars. After cleaning by iconv you can import the file.
I had this problem today and it was because inside of a TEXT column I had fancy quotes that had been copy/pasted from an external source.

Invalid byte sequence for encoding utf8 postgreSQL

When I try to do the following in the PSQL windows command shell:
INSERT INTO NAMES (surname) VALUES ('børre')
I get the following:
ERROR: invalid byte sequence for encoding "UTF8": 0x9b
Show client_encoding and show server_encoding gives "utf8".
Why cant the server utf8 encoding handle ø ? I've tried to change the client_encoding to latin1, which solves the problem in the terminal, but if I insert via python or other, the character isn't saved as utf8.

Convert file to UTF-8 without BOM using iconv on windows 8

I need to convert a bunch of files to:
UTF-8 without BOM
I have installed iconv:
http://www.gnu.org/software/libiconv/
But I can only find the plain UTF-8 option:
iconv --list
ANSI_X3.4-1968 ANSI_X3.4-1986 ASCII CP367 IBM367 ISO-IR-6 ISO646-US ISO_646.IRV:1991 US US-ASCII CSASCII
UTF-8
ISO-10646-UCS-2 UCS-2 CSUNICODE
So after I run the conversion the file is now in:
Is it not possible to convert to:
UTF-8 without BOM
using iconv?

Postgres using cp1252 encoding?

I have a postgres database that uses UTF-8 as encoding, and has client_encoding set to UTF8 as well. However, when using a script file that should be UTF8-encoded as well, it seems to assume the encoding is really cp1252, and gives me the following error:
FEHLER: Zeichen mit Byte-Folge 0x81 in Kodierung "WIN1252" hat keine Entsprechung in Kodierung "UTF8"
What is wrong here? Shouldn't the DB assume the file is in UTF8, instead of trying to convert it from cp1252? I even added the line
SET client_encoding='UNICODE';
But that didn't change anything (as said, the database is already configured that way...)
I had to manually insert the BOM, then it worked. (What the heck!)

Postgresql copying data into a table

I am using the copy command in Postgresql and I have a line of data in a text file that is tab seperated and I would like to copy it into the db table.
I get an error saying:
ERROR: invalid byte sequence for encoding "UTF8": 0x00
SQL state: 22021
Context: COPY real_acct1, line 113038
So I went to the line 113038 from the text file and copied it along with 4 or 5 neighboring lines into a new text file and behold that new data went in.
Any helpful thoughts? This is parcel data attributes info.
Your problem is actually one of character encoding.
The easiest way to deal with this is running your import data through iconv (assuming you're on a unix machine).
iconv -f original_charset -t utf-8 originalfile > newfile