[Python] UnicodeDecodeError when connecting to PostgreSQL using psycopg2, despite UTF-8...

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    EDIT : I’m running the code on Windows 11 (64-bit) with Python 3.11.9.

    The default language of my Windows system is French.

    I do not run the Django application in Chrome. I only use Chrome to view the pages generated by Django views.

    I have other browsers available for testing the application. I mentioned Chrome because after a browser update, it triggered an automatic reboot of my PC, which caused an abrupt shutdown of all servers (Django, database, etc.). The issues started right after this event, whereas everything was working fine before.

    The code snippet is part of the Django application. However, even when isolating and running this code on its own, the problem persists. Based on my observations, the issue seems to be specifically related to the database connection

    Continue reading...

Compartilhe esta Página