How to use Django with Sql Server - django

I normally use Postgres for my database needs with Django but I recently started at a company which use MSSQL on a Windows environment. Long story short I had to rewrite the database properties in settings.py. Unfortunately, I have NO idea how to connect to a SQL Server using Pyodbc and they're running Python 3.x so I can't use Django-Pyodbc. While trying to run it I'm getting a:
"Data source name not found and no default driver specified (0) (SQLDriverConnect)')"
Here is my current db config as it stands. I'm probably doing something wrong but it is very difficult to find resources since most Django+Sql Server results either use FreeTDS or Django-Pyodbc (neither are options).
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'db_name_on_server',
'USER': 'my_acct',
'PASSWORD': 'nope',
'HOST': 'x.x.x.x',
'PORT': '1433',
'OPTIONS': { # Options are not edited
'driver': 'SQL Server', # What it displays as on odbc admin
'dsn': 'System DSN', # What it displays as on odbc admin
'use_legacy_datetime': False
}

Old question, but it might help someone. These are the settings I use in Windows to connect to a SQL Server. As #flipperpa said, I also use django-pyodbc-azure
pyodbc==3.0.10
django-pyodbc-azure==1.10.0.1
However, the pyodbc that pip will download doesn't work for me. As suggested in this answer, go this site http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyodbc and download either depending on the version of Python 3.5 you have installed:
pyodbc‑3.0.10‑cp35‑none‑win32.whl if you have a 32-bit Python 3.5 install
pyodbc‑3.0.10‑cp35‑none‑win_amd64.whl if you have a 64-bit Python 3.5 install
Then use these settings to connect:
'default': {
'ENGINE': 'sql_server.pyodbc',
'HOST': "localhost",
'USER': "--user name--",
'PASSWORD': "--password--",
'NAME': "--database name--",
'PORT': 1433,
'OPTIONS': {
'driver' : 'SQL Server Native Client 11.0',
'MARS_Connection' : True,
'driver_supports_utf8' : True,
},
}
For anyone that is using linux, MS has now released official odbc drivers for SQL server and is officially (if more than a little tacitly) supporting the django-pyodbc-azure project. I highly recommend using it and also the native linux SQL Server ODBC driver over FreeTDS.

I've had my best luck with the following stack, and we're a Python 3 shop exclusively:
FreeTDS 0.95
pyodbc 3.0.10
django-pyodbc-azure 1.8.3 (assuming Django 1.8+)
Assuming you have odbc.ini, odbcinst.ini, and freetds.conf all squared away, here's an example of settings that work for me. Some of these depend on whether or not you're using SQL Server 2008+:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'HOST': 'yourserver.com',
'PORT': '1433',
'NAME': 'your_db',
'USER': 'your_user',
'PASSWORD': 'your_pw',
'AUTOCOMMIT': True,
'OPTIONS': {
'driver': 'FreeTDS',
'unicode_results': True,
'host_is_server': True,
'extra_params': 'tds_version=7.2;',
},
},
}
You'll also need to include 'use_legacy_datetime': True, if you're running SQL Server 2005 or less, otherwise, it will use the new SQL Server 2008+ date fields. It will also automatically set to true if you're using an outdated driver. Good luck!

Official backend fork from Microsoft: Microsoft MSSQL Django
They also have this reference Azure SQL Database Django sample.
Set 'ENGINE': 'mssql' to use it. Example:
DATABASES = {
"default": {
"ENGINE": "mssql",
"NAME": "mydb",
"USER": "user#myserver",
"PASSWORD": "password",
"HOST": "myserver.database.windows.net",
"PORT": "",
"OPTIONS": {
"driver": "ODBC Driver 17 for SQL Server",
},
},
}

Meanwhile there is django-mssql-backend for MSSQL support and this how my settings look like (Django 3.0, django-mssql-backend 2.8.1)
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': os.getenv('DB_NAME'),
'USER': os.getenv('DB_USER'),
'PASSWORD': os.getenv('DB_PASSWORD'),
'HOST': os.getenv('DB_HOST'),
'PORT': os.getenv('DB_PORT'),
'TEST': {
'NAME': os.getenv('DB_NAME'),
},
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
'extra_params': 'MARS_Connection=Yes'
},
}
}

Related

Adaptive Server is unavailable or does not exist

I'm trying to connect to mssql server via FreeTDS.
First I tried it via ODBC Driver 17 for SQL Server and it works. Here is my configuration in settings.py.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'mssql_database': {
'ENGINE': 'django_pyodbc',
'NAME': 'blabla',
'USER': 'blabla',
'PASSWORD': 'blabla',
'HOST': '10.65.1.20',
'PORT': '',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
},
}
According to this guide I installed FreeTDS on Ubuntu 18.04.
Here is my /etc/odbcinst.ini
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.9.so.1.1
UsageCount=1
[FreeTDS]
Description = TDS driver (Sybase/MS SQL)
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
CPTimeout =
CPReuse =
And here is the new settings.py section
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'mssql_database': {
'ENGINE': 'django_pyodbc',
'NAME': 'blabla',
'USER': 'blabla',
'PASSWORD': 'blabla',
'HOST': '10.65.1.20',
'PORT': '',
'OPTIONS': {
'driver': 'FreeTDS',
'host_is_server': True,
'extra_params': "TDS_VERSION=8.0"
},
},
}
And I have this error message
pyodbc.OperationalError: ('08S01', '[08S01] [FreeTDS][SQL Server]Unable to connect: Adaptive Server is unavailable or does not exist (20009) (SQLDriverConnect)')
How can I fix the error? The connection works with ODBC Driver 17 for SQL Server. So why doesn't it work with FreeTDS driver? Could it be because the file /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so isn't there? I can't find libtdsS.so.
$ pip list
Package Version
------------- -------
Django 1.8
django-pyodbc 1.1.3
pip 21.3.1
pyodbc 4.0.32
setuptools 59.6.0
sqlany-django 1.13
sqlanydb 1.0.11
wheel 0.37.1
As you noticed, set the PORT to 1433 - but that is only part of what you need to do.
TDS_Version=8.0 is invalid and will break on newer versions of FreeTDS greater than 1.3: https://www.freetds.org/userguide/ChoosingTdsProtocol.html
Since Ubuntu 18 ships with FreeTDS version 1.00.82, you should use version 7.4 of the TDS protocol, assuming you are using SQL Server 2012 or higher.
Change your options as follows:
'OPTIONS': {
'driver': 'FreeTDS',
'host_is_server': True,
'extra_params': "TDS_Version=7.4"
},
You may have to re-run your migrations, if any, because the newer TDS Versions support more SQL Server fields, such as DATE and DATETIME2 - but it looks like you may just be using SQL Server for reads. Good luck!

Running into Redis Connection error when adding a second queue to django-RQ

This issue is most likely because of my misunderstanding of how django-RQ/redis works.
I've been using django-rq with great results to run/cache long running processes. However, we're now in a position where we need to split up some of these processes into different queues.
The docs make this seem easy enough. However, I get the following error when trying to send a task to the pro queue: Could not resolve a Redis connection
I was thinking it was possibly because I'm using the same connection info for both queues, but I've seen other examples that do the same thing (https://newbedev.com/how-to-create-multiple-workers-in-python-rq).
Where did I go wrong? (I included the local and heroku settings as the same issue exists in both).
if(on_heroku):
RQ_QUEUES = {
'default': {
'URL': os.getenv('REDISTOGO_URL', 'redis://localhost:6379/0'), # If you're on Heroku
'DEFAULT_TIMEOUT': 500,
},
'pro': {
'URL': os.getenv('REDISTOGO_URL', 'redis://localhost:6379/0'), # If you're on Heroku
'DEFAULT_TIMEOUT': 500,
}
}
else:
RQ_QUEUES = {
'default': {
'HOST': 'localhost',
'PORT': 6379,
'DB': 0,
'DEFAULT_TIMEOUT': 500,
},
'pro': {
'HOST': 'localhost',
'PORT': 6379,
'DB': 0,
'DEFAULT_TIMEOUT': 500,
}
}

Why do I lose connectivity to my SQL Server when running through Apache?

I am trying to deploy my Django project on my company LAN. I am able to get the site working. However, I lose connectivity to my Microsoft SQL Server when I run the site through Apache. Everything works fine in the development environment. I suspect that I am losing Windows authentication when I work through the Apache server. This is what my DB settings are in my settings.py file:
'mosaiq': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'MOSAIQ',
'USER': '',
'PASSWORD': '',
'HOST': 'HHCMOSAIQ01T',
'PORT': '',
'OPTIONS': {'driver': 'ODBC Driver 11 for SQL Server',
},
Any idea how to connect to my SQL Server using Windows authentication or does my problem lie elsewhere?
This is the error I get:
('28000', "[28000] [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'MEDSTAR\$'. (18456) (SQLDriverConnect)")

Google App Engine (GAE) Improperly Configured Database

I have a working Django project that will deploy using Heroku. I am having trouble getting the app to deploy on GAE. When I run it locally, I get an error referring to an Improperly Configured database backend.
Any help would be appreciated.
Error:
...
raise ImproperlyConfigured(error_msg)
ImproperlyConfigured: 'postgresql' isn't an available database backend.
Try using django.db.backends.XXX, where XXX is one of:
'dummy', 'mysql', 'oracle', 'postgresql_psycopg2', 'sqlite3' <br>
Error was: No module named postgresql.base
...
app.yaml
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: main.app
libraries:
- name: django
version: "latest"
beta_settings:
cloud_sql_instances: <cloudsql-connection-string>
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'xxx',
'USER': '*****',
'PASSWORD': '******',
'HOST': 'xx.xx.xx.xx',
'PORT': '5432',
}
}
If I change the Engine to
'ENGINE': 'django.db.backends.postgresql_psycopg2'
I get the error:
ImportError: No module named psycopg2.extensions
pip freeze returns:
Django==1.11.4 psycopg2==2.7.3.1 pytz==2017.2
The GAE standard env. does not allow for psycopg2, and it seems that my original app.yaml (above) instructed GAE to allocate a standard env. instance. This disconnect was highlighted by Dan's comments (above). The correct procedure here is to 1) change the app to be able to deploy on GAE standard, or 2) change the app.yaml to deploy on a GAE flex (according to my current understanding). The following pages seem to help on the second option (https://cloud.google.com/appengine/docs/flexible/python/upgrading) and (https://cloud.google.com/appengine/docs/flexible/python/testing-and-deploying-your-app).

Does Django support pgpool?

Django db setting is
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
I want to know if I need to use pgpool
Should I need to change this settings ??
update
I try pgpool from this website
And I found that there is no need to modify the code
I don't know Django, but I know pgPool: you can connect to it just like you do with the standard Postgres server, no special config is needed.
Of course Django can connect and can use pgpool. If your pgpool is running on 127.0.0.1 at 5432 then for sure django can work with it. But I will recommend you not to use 5432 as pgppol port because it is the default port of postrgresql. You can use anyother port like 9999 or 7777 etc.
Follow this http://jensd.be/591/linux/setup-a-redundant-postgresql-database-with-repmgr-and-pgpool
to configure your postgresql in replication mode and failover.