r/SQL • u/thisismugisha • 2d ago
PostgreSQL Text column is storing accented text weirdly
/r/postgres/comments/1y3sx9v/text_column_is_storing_accented_text_weirdly/
0
Upvotes
r/SQL • u/thisismugisha • 2d ago
0
u/mirinai_ai 2d ago
The specific characters are a good clue. In Windows-1252, ã is byte 0xE3 and ó is 0xF3, and in the old DOS code page 437 that the Windows console uses by default, those same bytes are drawn as π and ≤. So it's quite possible the data is stored correctly and only the display is off, which is the classic psql-on-Windows case (psql warns that the console code page 437 differs from the Windows code page 1252).
You can check what's actually stored without trusting the display: SELECT col, octet_length(col), encode(convert_to(col, 'UTF8'), 'hex') FROM your_table WHERE ... If ã shows up as c3a3 and ó as c3b3 in the hex, the stored text is fine, and running chcp 1252 before starting psql (or viewing it in a GUI client) should show it correctly. If the hex is different, the conversion is happening in your application or driver on the way in.
The capital L in "Cat≤Lica" is a separate thing. With LC_CTYPE = 'C', functions like initcap, upper and lower only understand ASCII letters, so ó is treated as a word break and the next letter gets capitalized. If you use those functions on this column, a database created with a real locale (for example pt_BR.UTF-8, or ICU) avoids that.