alt text

WIth C# I know how to

  • connect to mssql using a micro orm (Dapper)
  • retry mssql using Polly

How to do this from Python side on Linux servers

Python Driver for SQL Server

Install ODBC then pyodbc




pip install pyodbc

# or add to Pipfile
pip install --upgrade pip

# gets pyodbc 4.0.39 which is most up to date as of Apr 14th 2023 (today is 10th July 2023)
pipenv update

First Query

From connect to SQL

# an easy way to import credentials
server = '' 
database = 'dbname' 
username = 'dave' 
password = 'secrethere' 

import cred_mssql
import pyodbc 
from loguru import logger

logger.debug(f'Trying to connect to db')
conn_string = 'DRIVER={ODBC Driver 18 for SQL Server};SERVER='+cred_mssql.server+';DATABASE='+cred_mssql.database+';ENCRYPT=yes;UID='+cred_mssql.username+';PWD='+ cred_mssql.password

# this will throw if it doesn't connect
cnxn = pyodbc.connect(conn_string)


I have had this error from an Ubuntu 20.04 server (on my local network running under Proxmox).

pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 18 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')

My WSL2 (Ubuntu 22.04) instance runs fine, as does cloud server on Ubuntu 20.04.

Turns out it was the pfsense firewall blocking the outbound 1433 port.

Production is an example of how I use this in prod. automated build script for a server.