Initializing a Database

The Database initialization method expects the name of the database as the first parameter. Subsequent keyword arguments are passed to the underlying database driver when establishing the connection, allowing you to pass vendor-specific parameters easily.

For instance, with Postgresql it is common to need to specify the host, user and password when creating your connection. These are not standard Peewee Database parameters, so they will be passed directly back to psycopg2 when creating connections:

  1. db = PostgresqlDatabase(
  2. 'database_name', # Required by Peewee.
  3. user='postgres', # Will be passed directly to psycopg2.
  4. password='secret', # Ditto.
  5. host='db.mysite.com') # Ditto.

As another example, the pymysql driver accepts a charset parameter which is not a standard Peewee Database parameter. To set this value, simply pass in charset alongside your other values:

  1. db = MySQLDatabase('database_name', user='www-data', charset='utf8mb4')

Consult your database driver’s documentation for the available parameters: