Slackbot
09/03/2025, 3:55 PMMarvin
09/03/2025, 3:56 PMMarvin
09/03/2025, 3:58 PMmssql+pyodbc, you must both:
1) Have a Microsoft ODBC driver installed on the machine/container where the task actually runs, and
2) Tell SQLAlchemy/pyodbc which driver to use via the URL query parameters.
In Prefect’s SqlAlchemyConnector you do the latter by setting query on ConnectionComponents.
Try this (note the query keys), replacing placeholders as needed:
from prefect_sqlalchemy import SqlAlchemyConnector
from prefect_sqlalchemy.credentials import ConnectionComponents, SyncDriver
connector = SqlAlchemyConnector(
connection_info=ConnectionComponents(
driver=SyncDriver.MSSQL_PYODBC, # produces mssql+pyodbc://...
username="username_placeholder",
password="pwd_placeholder",
host="host_placeholder", # add port if needed via port=1433
database="XXX",
query={
"driver": "ODBC Driver 18 for SQL Server", # must match an installed driver name
"Encrypt": "yes", # Azure SQL requires this
"TrustServerCertificate": "yes" # set "no" for production if you have proper certs
}
)
)
connector.save("com-sqlserver", overwrite=True)
def load_data():
with SqlAlchemyConnector.load("com-sqlserver") as connector:
data = connector.fetch_all("SELECT TOP 10 * FROM [XXX].[dbo].[Branches]")
return data
Key checks
- Verify the driver is actually installed in the runtime:
- In the same environment where the task runs:
import pyodbc
print(pyodbc.drivers())
Ensure you see exactly "ODBC Driver 18 for SQL Server" (or 17). Use that exact string in the query["driver"].
- If you’re running in a container or a separate worker image, the ODBC driver must be installed inside that image as well.
- If you use a named instance or non-default port, set port=1433 (or your port) on ConnectionComponents.
Install the Microsoft ODBC driver
- Windows: Microsoft ODBC Driver for SQL Server (Windows)
- macOS: Install on macOS
- Linux: Install on Linux
References
- Prefect SQLAlchemy integration: Prefect SQLAlchemy integration
- SQLAlchemy MSSQL + pyodbc details: SQLAlchemy MSSQL + pyodbc
If this still fails:
- What OS is the task running on (and is it inside Docker)?
- What’s the output of pyodbc.drivers()?
- Are you connecting to Azure SQL (often requires Encrypt=yes;TrustServerCertificate=no)?