How do I connect to SQL Server via sqlalchemy using Windows Authentication?

Question:

sqlalchemy, a db connection module for Python, uses SQL Authentication (database-defined user accounts) by default. If you want to use your Windows (domain or local) credentials to authenticate to the SQL Server, the connection string must be changed.

By default, as defined by sqlalchemy, the connection string to connect to the SQL Server is as follows:

sqlalchemy.create_engine('mssql://*username*:*password*@*server_name*/*database_name*')

This, if used using your Windows credentials, would throw an error similar to this:

sqlalchemy.exc.DBAPIError: (Error) ('28000', "[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for us
er '***S\username'. (18456) (SQLDriverConnect); [28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for us
er '***S\username'. (18456)") None None

In this error message, the code 18456 identifies the error message thrown by the SQL Server itself. This error signifies that the credentials are incorrect.

Asked By: vbiqvitovs

||

Answers:

In order to use Windows Authentication with sqlalchemy and mssql, the following connection string is required:

ODBC Driver:

engine = sqlalchemy.create_engine('mssql://*server_name*/*database_name*?trusted_connection=yes')

SQL Express Instance:

engine = sqlalchemy.create_engine('mssql://*server_name*\SQLEXPRESS/*database_name*?trusted_connection=yes') 
Answered By: vbiqvitovs

pyodbc

I think that you need to put:

“+pyodbc” after mssql

try this:

from sqlalchemy import create_engine

engine = create_engine("mssql+pyodbc://user:password@host:port/databasename?driver=ODBC+Driver+17+for+SQL+Server")

cnxn = engine.connect()

It works for me

Luck!

Answered By: Rodrigo Peña

If you’re using a trusted connection/AD and not using username/password, or otherwise see the following:

SAWarning: No driver name specified; this is expected by PyODBC when using >DSN-less connections
"No driver name specified; "

Then this method should work:

from sqlalchemy import create_engine

server = <your_server_name>

database = <your_database_name>

engine = create_engine('mssql+pyodbc://' + server + '/' + database + '?trusted_connection=yes&driver=ODBC+Driver+13+for+SQL+Server')
Answered By: Ray's Web Presence

A more recent response if you want to connect to the MSSQL DB from a different user than the one you’re logged with on Windows. It works as well if you are connecting from a Linux machine with FreeTDS installed.

The following worked for me from both Windows 10 and Ubuntu 18.04 using Python 3.6 & 3.7:

import getpass
from sqlalchemy import create_engine
password = getpass.getpass()
eng_str = fr'mssql+pymssql://{domain}{username}:{password}@{hostip}/{db}'
engine = create_engine(eng_str)

What changed was to add the Windows domain before username.
You’ll need to install the pymssql package.

Answered By: hyamanieu

Create Your SqlAlchemy Connection URL
     From Your pyodbc Connection String
     OR Your Known Connection Parameters

I found all the other answers to be educational, and I found the SqlAlchemy Docs on connection strings helpful too, but I kept failing to connect to MS SQL Server Express 19 where I was using no username or password and trusted_connection=’yes’ (just doing development at this point).

Then I found THIS method in the SqlAlchemy Docs on Connection URLs built from a pyodbc connection string (or just a connection string), which is also built from known connection parameters (i.e. this can simply be thought of as a connection string that is not necessarily used in pyodbc). Since I knew my pyodbc connection string was working, this seemed like it would work for me, and it did!

This method takes the guesswork out of creating the correct format for what you feed to the SqlAlchemy create_engine method. If you know your connection parameters, you put those into a simple string per the documentation exemplified by the code below, and the create method in the URL class of the sqlalchemy.engine module does the correct formatting for you.

The example code below runs as is and assumes a database named master and an existing table named table_one with the schema shown below. Also, I am using pandas to import my table data. Otherwise, we’d want to use a context manager to manage connecting to the database and then closing the connection like HERE in the SqlAlchemy docs.

import pandas as pd
import sqlalchemy
from sqlalchemy.engine import URL

# table_one dictionary:
table_one = {'name': 'table_one',
    'columns': ['ident int IDENTITY(1,1) PRIMARY KEY',
        'value_1 int NOT NULL',
        'value_2 int NOT NULL']}

# pyodbc stuff for MS SQL Server Express
driver='{SQL Server}'
server='localhostSQLEXPRESS'
database='master'
trusted_connection='yes'

# pyodbc connection string
connection_string = f'DRIVER={driver};SERVER={server};'
connection_string += f'DATABASE={database};'
connection_string += f'TRUSTED_CONNECTION={trusted_connection}'

# create sqlalchemy engine connection URL
connection_url = URL.create(
    "mssql+pyodbc", query={"odbc_connect": connection_string})

""" more code not shown that uses pyodbc without sqlalchemy """

engine = sqlalchemy.create_engine(connection_url)

d = {'value_1': [1, 2], 'value_2': [3, 4]}
df = pd.DataFrame(data=d)

df.to_sql('table_one', engine, if_exists="append", index=False)

Update

Let’s say you’ve installed SQL Server Express on your linux machine. You can use the following commands to make sure you’re using the correct strings for the following:

  1. For the driver: odbcinst -q -d
  2. For the server: sqlcmd -S localhost -U <username> -P <password> -Q 'select @@SERVERNAME'
Answered By: Thom Ives

If you are attempting to connect:
DNS-less
Windows Authentication for a server not locally hosted.
Without using ODBC connections.

Try the following:

import sqlalchemy

engine = sqlalchemy.create_engine('mssql+pyodbc://' + server + '/' + database + '?trusted_connection=yes&driver=SQL+Server')

This avoids using ODBC connections and thus avoids pyobdc interface errors from DPAPI2 vs DBAPI3 conflicts.

I would recommend using the URL creation tool instead of creating the url from scratch.

connection_url = sqlalchemy.engine.URL.create("mssql+pyodbc",database=databasename, host=servername, query = {'driver':'SQL Server'})

engine = sqlalchemy.create_engine(connection_url)

See this link for creating a connection string with SQL Server Authentication (non-domain, uses username and password)

Answered By: Isaacnfairplay
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.