Solving Error 28000: Login failed for user DOMAIN\\user with pyodbc in Python connection to SQL database

Error 28000: Login failed for user DOMAIN\\user with pyodbc in Python connection to SQL database is an error which occurs when you try to connect to an SQL database on python via window authentication

In this article I am going to help you solve this error and understand the root of the problem, also I am presenting other possible solutions that may work if the main solution does not work for you.

Explaining Error 28000: Login failed for user DOMAIN\\user with pyodbc in Python connection to SQL database

The error happens when you try to connect to an SQL database on python via window authentication.

First of all, let us check out how the message of the error looks like. The error message you are having should match this error message so we do not mix between problems and solutions.

                                                                       #
Error: ('28000', "[28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]
Login failed for user 'DOMAIN\\username'. (18456) (SQLDriverConnect); [28000] [Microsoft]
[SQL Server Native Client 11.0][SQL Server]Login failed for user 'DOMAIN\\username'. 
(18456)") 
                                                                       #

Th error above was generated after running the following code.

                                                                       #
cnxn = pyodbc.connect(driver='{SQL Server Native Client 11.0}', server='SERVERNAME', database='DATABASENAME', trusted_connection='yes')
                                                                       #

Bellow is a number of tested solutions that I have tried and that have worked for me.

Solution 1 : correctly connect to the database.

The first solution is to correctly connect to the database using the code bellow.

                                                                       #
import pyodbc
driver= '{SQL Server Native Client 11.0}'
cnxn = pyodbc.connect(
    Trusted_Connection='Yes',
    Driver='{ODBC Driver 11 for SQL Server}',
    Server='MyServer,1433',
    Database='MyDB')
                                                                       #

If the code above does not work, make sure to edit the code and remove the port number. That is going to solve the issue for most people.

Solution 2 : use the correct string.

In case the solution above does not solve your problem, we have another line of code that might work. The cxn string bellow may solve your issue. Try it and if it does not work, do not worry.

                                                                       #
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;PORT=1433;DATABASE=testdb;UID=me;PWD=pass')
                                                                       #

Try this one if the above does not work, make sure to edit the code and place your password.

                                                                       #
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=yourServer;DATABASE=yourDatabase;UID=yourUsername;PWD=yourPassword')
                                                                       #

The fixes above solve the issue for most developers.

If the solutions above helped you, consider supporting us on Kofi, any help is appreciated.

Summing-up

Thi is the end of our article, I hope you found our article and website useful, never give up, keep creating and keep coding. Errors are normal in our field, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/