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.

  1. psql_db = PostgresqlDatabase('my_database', user='postgres')
  2. class BaseModel(Model):
  3. """A base model that will use our Postgresql database"""
  4. class Meta:
  5. database = psql_db
  6. class User(BaseModel):
  7. username = CharField()

The Playhouse, extensions to Peewee contains a Postgresql extension module which provides many postgres-specific features such as:

If you would like to use these awesome features, use the PostgresqlExtDatabase from the playhouse.postgres_ext module:

  1. from playhouse.postgres_ext import PostgresqlExtDatabase
  2. psql_db = PostgresqlExtDatabase('my_database', user='postgres')