Initializing a Database
The Database
initialization method expects the name of the databaseas the first parameter. Subsequent keyword arguments are passed to theunderlying database driver when establishing the connection, allowing you topass 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 standardPeewee Database
parameters, so they will be passed directly back topsycopg2
when creating connections:
- db = PostgresqlDatabase(
- 'database_name', # Required by Peewee.
- user='postgres', # Will be passed directly to psycopg2.
- password='secret', # Ditto.
- host='db.mysite.com') # Ditto.
As another example, the pymysql
driver accepts a charset
parameterwhich is not a standard Peewee Database
parameter. To set thisvalue, simply pass in charset
alongside your other values:
- db = MySQLDatabase('database_name', user='www-data', charset='utf8mb4')
Consult your database driver’s documentation for the available parameters: