[Python] How to create a postgres user with sqlalchemy

Discussão em 'Python' iniciado por Stack, Novembro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I need to allow the creation of database users/roles through a web application that uses Python and SQLAlchemy in the backend.

    To do that i'm using something like:

    sql = 'CREATE USER :username WITH PASSWORD :passwd'
    sql_data = {
    "username": "the_user_name",
    "passwd": "the_password",
    }
    request.db.execute(sql, sql_data)


    But the SQL created by this query is:

    CREATE USER 'the_user_name' WITH PASSWORD 'the_password'


    and what I need is:

    CREATE USER "the_user_name" WITH PASSWORD 'the_password'


    with the username properly escaped. Is there a way that SQLAlchemy can handle this or should I create the string manually? If so, how can I escape the user input name?

    Continue reading...

Compartilhe esta Página