Hi Daniele,
On Wed, 14 Feb 2024 15:30:33 +0100
Daniele Varrazzo <daniele.varrazzo@gmail.com> wrote:
> Note however that if you just want to know the Python codec you can
> find it in `conn.info.encoding`
> (https://www.psycopg.org/psycopg3/docs/api/objects.html#psycopg.ConnectionInfo.encoding):
>
> >>> conn.info.encoding
> 'iso8859-1'
> >>> "€".encode(conn.info.encoding)
> ...
> UnicodeEncodeError: 'latin-1' codec can't encode character
> '\u20ac' in position 0: ordinal not in range(256)
Thanks very much for the help. Working directly with the encoding
of the server side, translated to python, is indeed a more
direct approach.
I did not use conn.info.encoding because the docs say that it
contains the _client_ encoding, not the server-side encoding
used to store the db content.
From the link above:
```
encoding
The Python codec name of the connection’s client encoding.
The value returned is always normalized to the Python codec name:
conn.execute("SET client_encoding TO LATIN9")
conn.info.encoding
'iso8859-15'
```
Confirming the encodings, connecting to the "latin1" db with psql shows:
```
$ psql -U kop latin1
psql (15.5 (Debian 15.5-0+deb12u1))
Type "help" for help.
kop_latin1=> show client_encoding;
client_encoding
-----------------
UTF8
(1 row)
kop_latin1=> show server_encoding;
server_encoding
-----------------
LATIN1
(1 row)
```
But, conn.info.encoding does return iso8859-1.
So I think your documentation has confused client
and server in this case. If you can confirm this
for me I'll go ahead and use conn.info.encoding.
Thanks again.
Regards,
Karl <kop@karlpinc.com>
Free Software: "You don't pay back, you pay forward."
-- Robert A. Heinlein