-
| Greetings, Has anyone had success connecting to PostgreSQL with Peewee? I've created a simple service that should connect to my PostgreSQL database (TimescaleDB add-on) but doesn't matter what I try I keep getting the following error: 
 I have added the  | 
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
| I'm sure it's a complex module; is it async? You will probably need to use it in a native python module perhaps also in its own thread. | 
Beta Was this translation helpful? Give feedback.
-
| For those running into the same issue, utilize the  from peewee import Model, CharField, PostgresqlDatabase
# Database configuration
DATABASE_CONFIG = {
    'host': 'timescaledb',
    'database': 'database_name',
    'user': 'user01',
    'password': 'password01',
    'port': 5432
}
# Create database instance
database = PostgresqlDatabase(**DATABASE_CONFIG)
@pyscript_executor
def connecting_to_postgresql():
        
        class User(Model):
            class Meta:
                database = database
            
            user = CharField(primary_key=True)
            email= CharField()
        database.connect()
        with database:
            database.create_tables( [User] , safe=True)
            User.create(user="user01", email="user01@example.com")
connecting_to_postgresql() | 
Beta Was this translation helpful? Give feedback.
For those running into the same issue, utilize the
@pyscript_executorand move the class models inside the function. Whether this is the right solutions, I do not know. I'm not a python nor a Pyscript expert but it works for my needs. Hope it helps others and thanks to the developers of Pyscript. I find this custom integration easier than some other solutions.