[Python] UTF-8 in psycopg3

Discussão em 'Python' iniciado por Stack, Abril 18, 2025.

  1. Stack

    Stack Membro Participativo

    I'm refactoring a python code base that is using psycopg2 to psycopg3.

    The code has calls to <cursor>.mogrify() which is not present in psycopg3.

    The code has something like this ensure that the strings are in UTF-8:

    arg_str = cur.mogrify("(%s,%s,%s,%s,%s)", x)
    args_list.append(arg_str)
    args_str = b','.join(args_list).decode("utf-8")
    cur.execute(insert_query + args_str)


    In the new codebase, I added client_encoding="UTF8" to the psycopg.connect() call

    Do I still need to do something like this ensure that strings will be in UTF8 when passing to execute()?

    cur.execute("INSERT INTO data(name) values (%s)", name.encode("utf-8").decode())

    Continue reading...

Compartilhe esta Página