# Multiple databases connections

**URL:** <https://forum.djangoproject.com/t/multiple-databases-connections/14422>\
**Category:** ORM\
**Created:** [June 21, 2022, 7:30pm UTC](https://forum.djangoproject.com/t/multiple-databases-connections/14422 "2022-06-21T19:30:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![angry\_ale](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/angry_ale/32/8987_2.png) [@angry\_ale](https://forum.djangoproject.com/u/angry_ale)\
**Post date:** [June 21, 2022, 7:30pm UTC](https://forum.djangoproject.com/t/multiple-databases-connections/14422/1 "2022-06-21T19:30:54Z")

</div>

Hey, so, I got an interesting request.

Context:

- We have multiple MSSQL databases in different servers (16 servers, 1 db each)
- We need to execute stored\_procedures on those MSSQL dbs multiple times a day on demand with different parameters
- We need to do it through a web interface and through endpoint calls

So, I was thinking using django because it’s easy to handle Auth, REST APIs and templates, but still not sure if the default DATABASES in the settings will be useful or not.

My approaches so far are:

1. Use django for Auth and API purposes, along pyodbc to connect and execute the sprocs in those MSSQL dbs
2. Same as 1 for Auth and API, but making independent services in flask to execute the sprocs for each server
3. Understand if I can use the settings DATABASES without the need of running any migration on those.

Any suggestion is much appreciated!

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 21, 2022, 7:35pm UTC](https://forum.djangoproject.com/t/multiple-databases-connections/14422/2 "2022-06-21T19:35:03Z")

</div>

Django supports multiple databases very well. See [Multiple databases | Django documentation | Django](https://docs.djangoproject.com/en/4.0/topics/db/multi-db/#multiple-databases).

You do not ever **need** to run migrations on _any_ database. It’s never a required operation. It only exists to make some (most?) changes easier to manage. If you don’t want Django to manage a database, then don’t.

(We regularly integrate external data sources into our Django projects. It works quite well.)

---

<div class="post-metadata">

**Author:** ![angry\_ale](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/angry_ale/32/8987_2.png) [@angry\_ale](https://forum.djangoproject.com/u/angry_ale)\
**Post date:** [June 21, 2022, 7:38pm UTC](https://forum.djangoproject.com/t/multiple-databases-connections/14422/3 "2022-06-21T19:38:44Z")

</div>

Thank you Ken, I will run some tests in the staging env so I can confirm this will work for me.

---

<div class="post-metadata">

**Author:** ![angry\_ale](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/angry_ale/32/8987_2.png) [@angry\_ale](https://forum.djangoproject.com/u/angry_ale)\
**Post date:** [June 22, 2022, 12:03pm UTC](https://forum.djangoproject.com/t/multiple-databases-connections/14422/4 "2022-06-22T12:03:08Z")

</div>

Update:  
Just found 2 connectors, but both have issues. Django-mssql doesn’t seem to work from a Unix server and django-pyodbc is pretty deprecated, doesn’t work in django 3 or newer.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 22, 2022, 12:07pm UTC](https://forum.djangoproject.com/t/multiple-databases-connections/14422/5 "2022-06-22T12:07:31Z")

</div>

See the complete thread at [Django3.0+ connects to SQL Server](https://forum.djangoproject.com/t/django3-0-connects-to-sql-server/2796)

It may also help to read [Install the Microsoft ODBC driver for SQL Server (Linux) - ODBC Driver for SQL Server | Microsoft Docs](https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver16)

---

<div class="post-metadata">

**Author:** ![angry\_ale](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/angry_ale/32/8987_2.png) [@angry\_ale](https://forum.djangoproject.com/u/angry_ale)\
**Post date:** [June 22, 2022, 12:37pm UTC](https://forum.djangoproject.com/t/multiple-databases-connections/14422/6 "2022-06-22T12:37:53Z")

</div>

Was trying with django-mssql-backend and it doesn’t throw any error! that’s a good progress! thank you for that!

Now I was trying to access that specific db to run the stored procedure… from the documentation, it says to use django.db.connection but I’m not sure how it will choose the connection… [django-mssql-doc](https://django-mssql.readthedocs.io/en/latest/usage.html#stored-procedures)

I know this is a little off-topic but you really look like an expert on this… Since I’m going to have 16 dbs configured in the settings, is there a way to pick a specific db connection to run my stored procedure?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 22, 2022, 12:42pm UTC](https://forum.djangoproject.com/t/multiple-databases-connections/14422/7 "2022-06-22T12:42:02Z")

</div>

Everything I know about [multiple databases](https://docs.djangoproject.com/en/4.0/topics/db/multi-db/) is located in the docs.

Without seeing what you’re trying to do, it would be difficult for me to offer any specific advice.

---

<div class="post-metadata">

**Author:** ![angry\_ale](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/angry_ale/32/8987_2.png) [@angry\_ale](https://forum.djangoproject.com/u/angry_ale)\
**Post date:** [June 22, 2022, 1:16pm UTC](https://forum.djangoproject.com/t/multiple-databases-connections/14422/8 "2022-06-22T13:16:35Z")

</div>

Thank you Ken, but that documentation is to use the ORM, in my case, I will not be dealing with it.  
Still, found that db.connections.databases and db.connections.all() contain all the information I need.

Let me thank you one more time, your inputs were a great help to me in solving this.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 22, 2022, 1:19pm UTC](https://forum.djangoproject.com/t/multiple-databases-connections/14422/9 "2022-06-22T13:19:42Z")

</div>

> [@angry\_ale](#):
>
> Thank you Ken, but that documentation is to use the ORM, in my case, I will not be dealing with it.

Not all of it. There’s a section in there for how to use the connections object to get a raw cursor for direct database access. ([Using raw cursors with multiple databases](https://docs.djangoproject.com/en/4.0/topics/db/multi-db/#using-raw-cursors-with-multiple-databases))

---

<div class="post-metadata">

**Author:** ![angry\_ale](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/angry_ale/32/8987_2.png) [@angry\_ale](https://forum.djangoproject.com/u/angry_ale)\
**Post date:** [June 22, 2022, 2:25pm UTC](https://forum.djangoproject.com/t/multiple-databases-connections/14422/10 "2022-06-22T14:25:11Z")

</div>

You are the best!!  
Thank you!
