Home >Backend Development >Golang >Incorrect UTF8 characters after selecting from postgres

Incorrect UTF8 characters after selecting from postgres

WBOY
WBOYforward
2024-02-12 19:48:091220browse

从 postgres 选择后 UTF8 字符不正确

php editor Apple sometimes encounters the problem of incorrect UTF8 characters after selection when using the postgres database. In this case, special characters stored in the database may appear as garbled or incorrect characters. The solution to this problem is to ensure the correct display of characters by correctly setting the database and connection character sets. When using a postgres database, we can solve this problem by modifying the configuration file or setting the character set when connecting to the database. Correctly setting the character set will ensure that we can correctly handle and display various characters and improve the stability and reliability of the database.

Question content

I have a database table in postgres that contains email addresses. One of the customers has an umlaut (ü) in her email address. This shouldn't be a problem, but somehow the string in go contained the wrong byte sequence (it was e3bc instead of c3bc), which later caused me a bunch of problems.

I use client_encoding=utf8 to connect to the database and the database is set to utf8. If I run the following command, I can see that the byte sequence in the database is as expected:

SELECT encode("email"::bytea, 'hex') FROM participants WHERE  email like 'XXXXXX%';
                    encode                    
----------------------------------------------
                     c3bc

(The rest of the data is hidden)

I'm using the database/sql package and the postgres driver to read the data, and if I print the string in go, I get xxxxxxe3bcxxxxxx, which is not what I expect (again, hiding the rest of the email with an x).

Is this a bug, or did I misunderstand something?

Solution

Make sure your database is correctly set to UTF8. Locale settings were fixed when creating the database, which could cause problems with sql functions such as LOWER. Re-create the database using pg_dropcluster and pg_createcluster --encoding=UTF8.

The above is the detailed content of Incorrect UTF8 characters after selecting from postgres. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete