So I see in psycopg 2, there is a set_client_encoding('UTF8')
function. In the psycopg 3 documentation it reads:
client_encoding is gone
Here I create the connection:
conn = psycopg.connect(conninfo, row_factory = dict_row, autocommit = autocommit, client_encoding="UTF8")
But then when I go to actually use the connection:
cur.execute(query, params)
I get an encoding error.
UnicodeEncodeError: 'charmap' codec can't encode character 'u202f' in position 15: character maps to <undefined> encoding with
‘cp1252’ codec failed
Using the l
in postgres, all databases are shown as WIN1252
. This is on Windows 10. The version of PostgreSQL 16.3. I am working with code which was developed on Linux and I am trying to get the same functionality working on Windows. I really would like to work in UTF-8.
How would I work around this in pyscopyg 3?
2
Answers
@klin sort of gave the right answer to the question being asked, and I always do like users who answer the question instead of whatever question the commenter/answerer wants to answer.
That being said, the best answer in this case, or the answer that applied to my case was to drop the database and recreate it with the required encoding and collation. This is acceptable to me because I can quickly repopulate all the data once a new table is created with a new encoding. One should do a dump before dropping if the data needs to be kept around for the new database.
So this create statement which solves the problem for me is as follows:
You should check what encoding is detected by psycopg, e.g.:
You can define the parameter in
conninfo
(dns):or with the Postgres
SET
command: