Django Snowflake Backend
django-snowflake is a Django database backend that enables Django applications to connect to and use Snowflake data warehouses. It's currently at version 6.0 and is actively maintained, primarily releasing new versions to support the latest Django releases and address Snowflake-specific features or issues.
Common errors
-
django.core.exceptions.ImproperlyConfigured: 'django_snowflake' isn't an available database backend. Try using 'django.db.backends.XXX', where XXX is one of: 'mysql', 'oracle', 'postgresql', 'sqlite3'.
cause The `django-snowflake` package is not installed or the `ENGINE` string in `settings.py` is misspelled.fixRun `pip install django-snowflake` and verify that `DATABASES['default']['ENGINE']` is set to `'django_snowflake'`. -
ModuleNotFoundError: No module named 'snowflake.connector'
cause The `snowflake-connector-python` dependency, required by `django-snowflake`, is not installed in your environment.fixInstall the underlying Snowflake connector: `pip install snowflake-connector-python`. -
django.db.utils.OperationalError: Failed to connect to DB: <your-account>.snowflakecomputing.com:443. ... Invalid username or password.
cause Incorrect credentials (user, password, account, role, warehouse) or network issues preventing connection to Snowflake.fixDouble-check your `USER`, `PASSWORD`, `ACCOUNT`, `WAREHOUSE`, and `ROLE` settings in `DATABASES['default']['OPTIONS']`. Ensure your network allows outbound connections to Snowflake endpoints. -
django.core.exceptions.ImproperlyConfigured: The DATABASE 'default' engine was missing the following key(s): NAME
cause The `NAME` parameter is missing from the `DATABASES` configuration, which is required by `django-snowflake` to specify the Snowflake database.fixAdd `"NAME": "your_snowflake_database"` to your `DATABASES['default']` configuration. Even when using a DSN, a top-level `NAME` might be expected or can be deduced from the DSN itself.
Warnings
- breaking django-snowflake v6.0 requires Django 5.x. Previous versions of django-snowflake supported older Django versions.
- gotcha The `TYPE: 'SNOWFLAKE'` option is no longer required in `DATABASES` settings for django-snowflake versions >= 5.0. Including it is harmless but unnecessary.
- gotcha When using a DSN (Data Source Name) in `OPTIONS`, ensure all required parameters (user, password, account, database, schema, warehouse, role) are correctly embedded or provided. Mixing DSN with individual options can lead to unexpected behavior.
- gotcha Snowflake's default session schema might not be 'public'. If your Django models expect a specific schema, you must define it explicitly in the `SCHEMA` option or within the DSN.
Install
-
pip install django-snowflake
Imports
- ENGINE setting
DATABASES = {'default': {'ENGINE': 'django_snowflake.base', ...}}DATABASES = {'default': {'ENGINE': 'django_snowflake', ...}}
Quickstart
import os
DATABASES = {
"default": {
"ENGINE": "django_snowflake",
"NAME": os.environ.get("SNOWFLAKE_DATABASE", "mydb"),
"USER": os.environ.get("SNOWFLAKE_USER", "my_user"),
"OPTIONS": {
"ACCOUNT": os.environ.get("SNOWFLAKE_ACCOUNT", "myaccount"),
"PASSWORD": os.environ.get("SNOWFLAKE_PASSWORD", ""),
"WAREHOUSE": os.environ.get("SNOWFLAKE_WAREHOUSE", "my_warehouse"),
"ROLE": os.environ.get("SNOWFLAKE_ROLE", "my_role"),
# Optional: Specify the schema if not using a DSN that includes it
"SCHEMA": os.environ.get("SNOWFLAKE_SCHEMA", "public")
# For key-pair authentication, provide PRIVATE_KEY instead of PASSWORD:
# "PRIVATE_KEY": os.environ.get("SNOWFLAKE_PRIVATE_KEY", ""),
# "PRIVATE_KEY_PASSPHRASE": os.environ.get("SNOWFLAKE_PRIVATE_KEY_PASSPHRASE", "")
},
}
}
# Example of using a DSN (Data Source Name) instead of separate options:
# DATABASES = {
# "default": {
# "ENGINE": "django_snowflake",
# "NAME": os.environ.get("SNOWFLAKE_DATABASE", "mydb"),
# "OPTIONS": {
# "DSN": os.environ.get(
# "SNOWFLAKE_DSN",
# "snowflake://{user}:{password}@{account}.snowflakecomputing.com/{database}/{schema}?warehouse={warehouse}&role={role}"
# ).format(
# user=os.environ.get("SNOWFLAKE_USER", "my_user"),
# password=os.environ.get("SNOWFLAKE_PASSWORD", ""),
# account=os.environ.get("SNOWFLAKE_ACCOUNT", "myaccount"),
# database=os.environ.get("SNOWFLAKE_DATABASE", "mydb"),
# schema=os.environ.get("SNOWFLAKE_SCHEMA", "public"),
# warehouse=os.environ.get("SNOWFLAKE_WAREHOUSE", "my_warehouse"),
# role=os.environ.get("SNOWFLAKE_ROLE", "my_role")
# )
# },
# }
# }