3

I am having trouble figuring out the configuration steps and code that I need to access a SQL Server database via Windows Authentication from a Mac.

On the Windows machine I can use the following code:

import getpass

import pypyodbc

cnxn_string = (r"DRIVER={ODBC Driver 13 for SQL Server};"
               r"SERVER=[Host]\[Instance];"
               r"DATABASE=[Database]"
               r"UID=[Domain]\[Username];"
               r"Trusted_Connection=yes;"% (getpass.getuser()))

cnxn = pypyodbc.connect(cnxn_string)

But on a Mac I believe I need to re-enter my username and password.

How can I create a similar python connection to the SQL Server, via Windows Authentication, on my mac? I am open to any python modules (it does not have to be pypyodbc).

2
  • The mac is in the same AD? Commented Jun 1, 2017 at 13:33
  • I believe so, because I am able to access the database from my mac using a database client (DBVisualizer), but in the client I need to re-enter my username and password and select the "Windows" authentication method. Commented Jun 1, 2017 at 13:51

1 Answer 1

3

Using FreeTDS + pymssql it's pretty trivial to connect.

Note: I would recommend using FreeTDS 1.0+ and pymssql 2.2.0+ because there are compatibility issues with FreeTDS 1.0 and pymssql 2.1.3.

Here's an example using pymssql:

import pymssql
pymssql.connect(server="myserver\\myinstance",
                user="mydomain\\myuser",
                password="mypassword",
                database="mydatabase")

Note: the instance is parsed from the server using the backslash character, but we need to escape this because this is also the escape character. The same can be said for parsing the Active Directory domain from the user.

Sign up to request clarification or add additional context in comments.

3 Comments

awesome. this doesn't work with pypyodbc as far as i can tell
@mgoldwasser You said that we should use pymssql 2.2.0+ version. I checked latest version: pymssql.org/en/stable/changelog.html and it's 2.1.4. This version is ok? I have problem during connection (pymssql 2.1.4, freeTDS 1.00.111). I get error during connection: Net-Lib error during Operation timed out (60)\n').
Hmm, I guess they removed the 2.2.0+ branch from the project GitHub. I'm not sure why...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.