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:
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
parameter which is not a standard Peewee Database
parameter. To set this value, 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: