Search code examples
pythonpostgresqlpeewee

How to set or select postgres' current_settings with peewee?


I have a postgres database that I want to query with peewee in python. If I connect to the database directly (psql or pgadmin) I can do something like

set my.setting='test'

or

select current_setting('my.setting')

How can I do this with peewee? The model I have contains only the tables I have in my database.

Thanks for help!


Solution

  • You can execute raw SQL using the Database method execute_sql(), example:

    db = PostgresqlDatabase("test")
    db.execute_sql("set my.setting to 'test'")
    cur = db.execute_sql("show my.setting")
    print(cur.fetchone()[0])