El Dj
El Dj

Reputation: 385

How to connect to snowflake database from Django framework

I'm new to Django and I'm trying to display the result that comes from a Snowflake database. I know that Django has multiple built-in database backend engines like: django.db.backends.postgresql and django.db.backends.mysql among the other few it supports.

Unfortunately, I couldn't find a proper way of configuring a database backend engine in the

settings.py

When I enter sqlalchemy or snowflake-sqlalchemy as the engine, I get this error:

Try using 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'postgresql', 'sqlite3'

My guess was to go with sqlalchemy as that's what I usually use to connect to Snowflake outside of Django but for some reason, it's not working properly.

I'd appreciate any guidance on that.

Upvotes: 6

Views: 4392

Answers (3)

Felipe Hoffa
Felipe Hoffa

Reputation: 59175

2022 update: There's now a Snowflake backend for Django funded by Snowflake customers and implemented by Django's Tim Graham:

From their docs:

Install and usage

Use the version of django-snowflake that corresponds to your version of Django. For example, to get the latest compatible release for Django 3.2.x:

pip install django-snowflake==3.2.*

The minor release number of Django doesn't correspond to the minor release number of django-snowflake. Use the latest minor release of each.

Configure the Django DATABASES setting similar to this:

DATABASES = {
    'default': {
        'ENGINE': 'django_snowflake',
        'NAME': 'MY_DATABASE',
        'SCHEMA': 'MY_SCHEME',
        'WAREHOUSE': 'MY_WAREHOUSE',
        'USER': 'my_user',
        'PASSWORD': 'my_password',
        'ACCOUNT': 'my_account',
    },
}

Some of the discussion while implementing it:

Upvotes: 4

francoisaubert
francoisaubert

Reputation: 21

You should install a custom Snowflake engine like the following ones. Note that, as of today, those are incomplete. Though, it should not be difficult to implement missing Django features by completing the operations.pyfile.

-> https://github.com/pricemoov/django-snowflake

or

-> https://pypi.org/project/django-snowflake-backend/

Upvotes: 2

Ankur Srivastava
Ankur Srivastava

Reputation: 923

please install snowflake-connector-python .E.g. below
pip3 install snowflake-connector-python==1.8.1
Here is the code to connect from SQL Alchemy.

=====================================================================
#!/usr/bin/env python
from snowflake.sqlalchemy import URL
from sqlalchemy import create_engine

engine = create_engine(URL(
    account = 'XXXX',
    user = 'XXXX',
    password = 'XXXXX',
    database = 'XXXXXX',
    schema = 'XXXXXX',
    warehouse = 'XXXXX',
    role='XXXXXXXX',
))




try:
    connection = engine.connect()
    connection.execute(
        "CREATE OR REPLACE TABLE test_async(c1 TIMESTAMP_NTZ,c2 VARIANT)",_no_results=True)



finally:
    connection.close()
    engine.dispose()

=========================================================================

Upvotes: -4

Related Questions