MS SQL on macOS: Connect to Your Database Using Python

Connect to MS SQL Server from Python on macOS: install Microsoft's ODBC driver with Homebrew, set up a DSN with unixODBC, and test it with pyodbc.

On this page

Updated September 2026: ODBC Driver 18 instead of the old Driver 13 preview, the current Homebrew commands, and the encryption settings Driver 18 needs.

In this article series, I’ll review the tools and options to connect to an MS SQL server on macOS.

Table of contents:

Microsoft provides an ODBC driver for macOS that works on both Intel and Apple Silicon Macs. Here is a quick and easy guide to connecting to your MS SQL using python.

Install Homebrew

The first thing you need is to install Homebrew.

Enter the following command in a terminal window to install Homebrew:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Install the ODBC Driver

Afterward, enter the following commands in a terminal window to install Microsoft ODBC Driver 18 for SQL Server:

brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
HOMEBREW_ACCEPT_EULA=Y brew install msodbcsql18 mssql-tools18

HOMEBREW_ACCEPT_EULA=Y accepts Microsoft’s license, which the install asks for. mssql-tools18 is optional. It adds the sqlcmd command-line tool, which is handy for testing. Homebrew also installs unixODBC, the ODBC driver manager, as a dependency.

Install pyodbc

Next item you need to install is pyodbc by entering the following command in a terminal:

pip install pyodbc

Now you have all the prerequisites to connect to your MS SQL database in Python.

Connect Without a DSN

The simplest way is to put everything in the connection string:

import pyodbc

conn = pyodbc.connect(
    "DRIVER={ODBC Driver 18 for SQL Server};"
    "SERVER=yourserver.database.windows.net;"
    "DATABASE=yourDatabase;"
    "UID=yourUserName;"
    "PWD=yourPassWord;"
)
print(conn.execute("SELECT @@VERSION").fetchone()[0])

Replace the server, database, username and password with your own. The script prints the SQL Server version if the connection works.

Driver 18 encrypts the connection by default and checks the server’s certificate. Azure SQL has a valid certificate, so this works as is. A local SQL Server or one in Docker usually has a self-signed certificate, and the connection fails with a certificate error. For those, add this to the connection string:

    "TrustServerCertificate=yes;"

Configure a DSN (Optional)

If you connect to the same server often, you can save its settings as a DSN (data source name). First, find where unixODBC keeps its configuration files:

odbcinst -j

The output lists two files:

  1. odbcinst.ini, the installed drivers
  2. odbc.ini, the data sources

Check the driver name in odbcinst.ini:

cat "$(brew --prefix)/etc/odbcinst.ini"

There you will see the following information:

[ODBC Driver 18 for SQL Server]
Description=Microsoft ODBC Driver 18 for SQL Server
Driver=/opt/homebrew/lib/libmsodbcsql.18.dylib
UsageCount=1

On Intel Macs, the driver path starts with /usr/local instead of /opt/homebrew. You need the copy the content in the square brackets which in my case is “ODBC Driver 18 for SQL Server”.

Now open a new file like this:

nano ~/tempfile

Add the following lines to this file:

[MSSQL]
Description = Test to SQLServer
Driver = ODBC Driver 18 for SQL Server
Server = YourSQLServerAddress
Database = YourDatabase

Write your SQL server address instead of YourSQLServerAddress and your database name instead of YourDatabase. For a server with a self-signed certificate, also add TrustServerCertificate = yes. Save the file and exit Nano editor.

Now, enter the following command in the terminal to register the DSN for your user:

odbcinst -i -s -f ~/tempfile -h

Test the Connection

To test your connection, open python editor and run the following script:

# pip install pyodbc

import pyodbc

conn = pyodbc.connect("DSN=MSSQL;UID={user_name};PWD={password}".format(
    user_name="yourUserName",
    password="yourPassWord")
    )
print(conn)

DSN is the name you used in the temp file. Replace yourUserName and yourPassWord with the ones you use for your SQL server.

You will see an output similar to:

<pyodbc.Connection object at 0xfffffffff>

You can also test the same server from the terminal with sqlcmd:

sqlcmd -S YourSQLServerAddress -d YourDatabase -U yourUserName -Q "SELECT @@VERSION"

sqlcmd asks for the password. Add -C to trust a self-signed certificate.

Written by Sina Fathi-Kazerooni

Lead AI Engineer. I write about reinforcement learning, transformers, multi-agent systems, and the tools I build along the way.