62

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.

vbiqvitovs
  • 1,533
  • 3
  • 12
  • 17

6 Answers6

59

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') 

UPDATE:

@DoloMike noticed that the previously provided solution is outdated and may not work as expected. Here's the corrected solution based on his comment below:

engine = create_engine('mssql+pyodbc://@' + servername + '/' + dbname + '?trusted_connection=yes&driver=ODBC+Driver+13+for+SQL+Server')

Adjust the ODBC Driver version based on your installed sql server.

Tbroeth
  • 345
  • 2
  • 12
vbiqvitovs
  • 1,533
  • 3
  • 12
  • 17
  • 4
    The above connection strings might be useful if we are running the script from the machine where we logged in using windows credentials. what if I need to run the script from the some other host? – Ravi Chandra Dec 22 '17 at 09:30
  • This is exactly what I am dealing with now - thus far, I am running into errors. – Didier Jean Charles Mar 21 '18 at 19:27
  • @RaviChandra, If you are running the scripts in interactive mode using an IDE, you could open the IDE as a different user. – steadyfish Jun 13 '18 at 21:57
  • What if it is used using pymssql ? Tried just adding `+pyssmql` after `mssql` but that does not work – CutePoison Jan 03 '19 at 10:28
  • 47
    Unfortunately this didn't work for me, but it led me to the right path. This is what I had to use: `sqlcon = create_engine('mssql+pyodbc://@' + servername + '/' + dbname + '?trusted_connection=yes&driver=ODBC+Driver+13+for+SQL+Server')` – DoloMike Jan 09 '19 at 15:31
  • 3
    @DoloMike Yeah, this answer is outdated. – NoName Feb 10 '20 at 05:08
  • 2
    @DoloMike I wish I could upvote your post 10,000 more times. – Chicken Sandwich No Pickles May 26 '20 at 06:18
  • what is the servername contains "\". With pyodbc, it is ok with `server='serveur\part_2'`, but with sqlalchemy, it is not working – John Smith Nov 18 '22 at 14:09
26

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')
Ray's Web Presence
  • 464
  • 1
  • 5
  • 15
9

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.

hyamanieu
  • 1,055
  • 9
  • 25
8

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='localhost\SQLEXPRESS'
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'
Thom Ives
  • 3,642
  • 3
  • 30
  • 29
  • 1
    To explain your URL.create function, it might be worthwhile to add some more details. For instance you can use the following:url = URL.create("mssql+pyodbc" , host='servername' , database="database Name" , query={"Trusted_Connection":'yes',"driver":"ODBC Driver 17 for SQL Server"}) – brian_ds Jan 20 '22 at 21:51
  • 1
    note that the query works for everything after the ? in the url. The named parameters work with the required info before that. – brian_ds Jan 20 '22 at 22:05
5

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!

Rodrigo Peña
  • 71
  • 1
  • 3
  • This doesn't answer the question about using "Windows Authentication" though it is correct you need the pyodbc as part of the connection string. – user8128167 Dec 05 '19 at 23:35
1

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)

Isaacnfairplay
  • 217
  • 2
  • 18