# postgres and UTF8 [solved]

## blue_calling

Hi,

I'm having problem with postgres which doesn't accept swedish letters. The problem turn upp in this example

```
SELECT * FROM images WHERE directory = 'Öland';

ERROR: invalid byte sequence for coding "UTF8": 0xf66c616e

HINT: This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client_encoding".
```

This is my config

```
psql -l -U secretuser

NAME       |  Owner        |  Encoding

-----------------------------------------

postgres  |  postgres      | UTF8

img_db    |  secretuser   | UTF8             <= the db with the images tables
```

```
locale

LANG=sv_SE.UTF-8

LC_CTYPE=sv_SE.UTF-8

LC_COLLATE=sv_SE.UTF-8

LC_MESSAGES=en_GB.UTF-8

LC_ALL=
```

I dont know if it's an issue, when listing directories with swedish letters they turn upp with a question mark. The above directory in the psql search looks in the system like this

```
ls

?land
```

while it looks perfectly ok "Öland" when accessing the directory from a FTP client with MS Win.

Can anyone tell what I'm doing wrong?

Thanks!Last edited by blue_calling on Sun Jan 17, 2010 6:41 pm; edited 1 time in total

----------

## pactoo

Just guessing... maybe the shell from which this statement is run is not UTF8 but ISO8859-x? Not sure, why you haven't set LC_ALL and what consequences this has for the running script. 

Then the "Ö" would have a different encoding, which may lead to the error.  

Even worse, for directory listings, unless you use JFS as filesystem with the mount option "iocharset=utf8" you cannot be sure, that your file on disc is UTF encoded. 

You could have two files in the same directory, one ISO and and one UTF encoded, which both have the same non ascii character but are displayed differently by ls - as those are encoded differently.

----------

## blue_calling

Thanks, appreciate your respons. Although I don't know how to get out of the problem  :Sad: 

I guess the first I should do is to check what charset the console use. Googleing says that I should make shore that I have "LANG=sv_SE.uft8" in locale, which I do. Do you know how to check it? I usually ssh from a MS Win pc with PuTTY.

I get the same error when running the script with the PSQL code from cron (root), syslog-ng started script and console. At least I guess, since the scripts always work when using a-z, but fails when å, ä and ö is involved.

----------

## blue_calling

Solved

I did a bunch of things to get out of the problem:

1. I did login with ISO 8859-1, hence changed the setting in my PuTTY. 

2. Revisited the Gentoo localization guide and run localedef for sv_SE and en_GB (I missed those)

3. Reemerge glibc postgresql (don't know if this was necissary, but I did it just in case)

----------

