r/postgres • u/thisismugisha • 6d ago
Debugging Text column is storing accented text weirdly
I have a column of type TEXT where I could store text such as “Quarteirão” or “Católica”. However, the text is actually being stored as “Quarteirπo”, or “Cat≤Lica”
The SQL of the database goes something like:
ENCODING = 'UTF8'
LC_COLLATE = 'C'
LC_CTYPE = 'C'
LOCALE_PROVIDER = 'libc'
TABLESPACE = pg_defaultand the column in question is:
column text COLLATE pg_catalog."default" NOT NULL,I'm new to PostgreSQL, so I don't know what to change to store the text as is. I did check whether it was being changed before being inserted into the database, and it isn't being changed. It is the database that's not storing it how I expect it to.
1
Upvotes
2
u/AtlantaRene 6d ago edited 6d ago
This is a shot in the dark but it looks like an encoding error. If you are using Windows and the driver may be trying to stick “Windows-1252 / ISO-8859-1” into the database. I rarely use Windows but checking it looks like the connection string could include “Client_Encoding={value}” to do conversions.
1
u/addy3006 6d ago
The π and ≤ are a clue. Those are what bytes 0xE3 and 0xF3 (ã and ó in Latin-1/Windows-1252) look like in the old DOS code page 437. So I'd suspect the client or console encoding rather than the column or the C collation. LC_COLLATE/LC_CTYPE = 'C' affects sorting and case, not storage, and your UTF8 database is fine. Check what's actually stored with
SELECT col, encode(convert_to(col,'UTF8'),'hex') FROM t WHERE ...and look for c3a3 for ã. If that's what you see, the data is right and the display is wrong. If not, checkSHOW client_encoding;in the session doing the insert and set it to match what your app actually sends.