9

Is there a way for python to connect to MS SQL Server using Windows Authentication, even when not running the python app on a windows box?

I'm trying to do this with pymssql, but the examples mostly seem to assume that you're running on windows.

If there is a way to make this connection using some other library, please feel free to suggest, but I do like how pymssql is simple to install and deploy via pip.

I want to connect to 2005/2008 databases, and I'm running Ubuntu 13.04 (but I can upgrade to a later Ubuntu if it makes a difference)

SOLUTION:

It turns out that pymssql can connect to my database via my windows username and password. But to do so, I need to pass the actual username/password like this:

pymssql.connect(host, 'THEDOMAIN\\theusername', 'thepassword', db)

The solution EkoostikMartin provided is still good though (if you do not want to store a password somewhere, which is probably the point to windows auth anyway)

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
Dan
  • 3,665
  • 1
  • 31
  • 39
  • 1
    I think this still requires the SQL Server configured to allow 'mixed' authentication -- without the Kerberos token, you're not using Integrated Windows Authentication, even if the credentials are the same. – northben May 13 '15 at 20:01
  • With Windows 10, that password could be your entire microsoft account password. It might a good idea to enable the `sa` user with a custom password, instead of using your microsoft account. – CMCDragonkai Jan 30 '17 at 11:33

1 Answers1

8

You can use the SQL Server ODBC driver for linux, and set up Kerberos.

See this article - http://technet.microsoft.com/en-us/library/hh568450.aspx

EkoostikMartin
  • 6,831
  • 2
  • 33
  • 62
  • 4
    this is actually pretty simple - I installed the pyodbc python package, then connected to the SQL Server like this: `pyodbc.connect("Driver={SQL Server};Server=servername\instancename;Trusted_Connection=yes;database=databasename")` – northben May 13 '15 at 20:08