Search code examples
djangodjango-database

How to config initial connection command for Django databases?


Can I do something like this in Django settings file:

DATABASE = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'test.sqlite3',
        'INITIAL_CMD': 'PRAGMA journal_mode=WAL',  # here
     }
}

so that whenever Django connects to default SQLite3 DB, it issues PRAGMA journal_mode=WAL first before others.
And yes, for those familiar with PHP, I need something like MYSQL_ATTR_INIT_COMMAND in MySQL PDO


Solution

  • You can use the connection_created signal.