r/postgres • • 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_default

and 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

8 comments sorted by

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/thisismugisha 6d ago

I am using windows!

How do I fix this tho?

1

u/AtlantaRene 6d ago edited 6d ago

Did you write a program to insert the data into the database, or are you using a program like psql?

If you are using psql, this may be a reference for you to review that may help you resolve the problem:

https://dbakevlar.com/2026/02/postgresql-with-windows-and-the-code-page-trap-no-one-warned-you-about/

1

u/thisismugisha 6d ago

I'm using Nodejs. There's a specific package called pg-promise that I'm using.

1

u/AtlantaRene 6d ago

OK, Let’s confirm what we already suspect. Please do two separate queries:

SHOW server_encoding;

SHOW client_encoding;

pg-promise has a function called one which should let you obtain the value of each result.

Additionally, what are you trying to do? I assume that you have some data that you’re trying to put into PosgreSQL. Is this data from another database, if so, what type of database? Or is it from a text file or something that you are entering in your program?

2

u/gumnos 6d ago

The specifics would depend on the language you're using (C/C++/C#, Visual Basic, Python, Ruby, PHP, whatever), how it stores strings, and how it tells the (likely ODBC) driver what the encoding should be. But u/AtlantaRene's "shot in the dark" was dead on—it's an encoding mismatch at some point in the process.

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, check SHOW client_encoding; in the session doing the insert and set it to match what your app actually sends.