UnicodeDecodeError when connecting to PostgreSQL using psycopg2, despite UTF-8 encoding everywhere

I'm trying to connect to a local PostgreSQL database using psycopg2 in Python. Here's the code I'm using:

import psycopg2
    
    params = {
        'dbname': 'database_name',
        'user': 'user_name',
        'password': 'mypassword',
        'host': 'localhost',
    }
    
    for k, v in params.items():
        print(f"{k}: {v} (type={type(v)}, encoded={v.encode('utf-8')})")
    
    try:
        conn = psycopg2.connect(**params)
        print("Connexion OK")
        conn.close()
    except Exception as e:
        print("Connexion Erro")
        print(type(e), e)

The printed output confirms that all parameters are strings and UTF-8 encoded. However, I still get the following error:

    Connexion Erro
<class 'UnicodeDecodeError'> 'utf-8' codec can't decode byte 0xe9 in position 103: invalid continuation byte

I also checked the server and client encoding on PostgreSQL using:

   SHOW server_encoding;
   SHOW client_encoding;

Both return UTF8.

Given that all inputs are UTF-8 and the database is configured for UTF-8, I don't understand why this error occurs.

Has anyone encountered this before or has an idea of where this byte 0xe9 might come from? What else should I check?

Вернуться на верх