Using Postgresql
To connect to a Postgresql database, we will use PostgresqlDatabase
. The first parameter is always the name of the database, and after that you can specify arbitrary psycopg2 parameters.
psql_db = PostgresqlDatabase('my_database', user='postgres')
class BaseModel(Model):
"""A base model that will use our Postgresql database"""
class Meta:
database = psql_db
class User(BaseModel):
username = CharField()
The Playhouse, extensions to Peewee contains a Postgresql extension module which provides many postgres-specific features such as:
- Arrays
- HStore
- JSON
- Server-side cursors
- And more!
If you would like to use these awesome features, use the PostgresqlExtDatabase
from the playhouse.postgres_ext
module:
from playhouse.postgres_ext import PostgresqlExtDatabase
psql_db = PostgresqlExtDatabase('my_database', user='postgres')