SQLAlchemy can't connect to an mssql database SQLAlchemy can't connect to an mssql database sql-server sql-server

SQLAlchemy can't connect to an mssql database


If not specified in the URL, the default driver for the mssql+pyodbc dialect would be "SQL Server" [1]. That means you need to have a section that reads like this in /etc/unixODBC/odbcinst.ini:

[SQL Server]Driver=/path/to/library.so

It works "automatically" on Windows, because if you open Administrator Tools -> Data Sources (ODBC), you would most likely find an entry named "SQL Server" under the Drivers tab.

On Linux, you can either use the FreeTDS driver, or the official driver from Microsoft (I recommend this).

After installing the driver, you should have something like this in /etc/unixODBC/odbcinst.ini:

[FreeTDS]Driver=/usr/lib/libtdsodbc.soThreading=1[ODBC Driver 11 for SQL Server]Description=Microsoft ODBC Driver 11 for SQL ServerDriver=/opt/microsoft/msodbcsql/lib64/libmsodbcsql-11.0.so.2270.0Threading=1UsageCount=1

Then, you just have to add a driver query string parameter to the URL, with value that matches the section name.

Sample URL with FreeTDS:

mssql+pyodbc://user:pass@ip_address/database_name?driver=FreeTDS

Sample URL with the official driver:

mssql+pyodbc://user:pass@ip_address/database_name?driver=ODBC+Driver+11+for+SQL+Server

[1] https://bitbucket.org/sqlalchemy/sqlalchemy/src/aa3a8f016f3e4396d125b18b0510abdf72aa8af2/lib/sqlalchemy/dialects/mssql/pyodbc.py?at=default#cl-236


The error you are receiving may indicate there is no DSN setup with the name IM002. Have you tried testing the ODBC connection directly to validate that it is setup properly? Do you have the appropriate Microsoft SQL Server database drivers installed?