I'm trying to dynamically create users in a database like so:
from sqlalchemy.sql import text
def create_user(user: str, password: str):
stmt = text("CREATE USER :user WITH PASSWORD :password")
engine.execute(stmt, user=user, password=password)
create_user("bob", "password123")
But the problem with this is that SQLAlchemy will parameterize this query as:
CREATE USER 'bob' WITH PASSWORD 'password123'"
Which throws an error:
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.SyntaxError) syntax error at or near "'bob'"
LINE 1: CREATE USER 'bob' WITH PASSWORD 'password123'
This error is thrown because bob is supposed to be unquoted. The correct SQL output should in fact be:
CREATE USER bob WITH PASSWORD 'password123'
How can I prevent SQLAlchemy from wrapping the parameterized value in quotation marks?