MSSQL Connection¶
The MSSQL connection type enables connection to Microsoft SQL Server.
Default Connection IDs¶
MSSQL Hook uses parameter mssql_conn_id for the connection ID. The default value is mssql_default.
Configuring the Connection¶
- Host (required)
The host to connect to.
- Schema (optional)
Specify the schema name to be used in the database.
- Login (required)
Specify the user name to connect.
- Password (required)
Specify the password to connect.
- Port (required)
The port to connect.
- Extra (optional)
Specify the extra parameters (as json dictionary) that can be used in MSSQL connection.
More details on all MSSQL parameters supported can be found in MSSQL documentation.
When specifying the connection as URI (in AIRFLOW_CONN_{CONN_ID} variable) you should specify it
following the standard syntax of DB connections - where extras are passed as parameters
of the URI. Note that all components of the URI should be URL-encoded.
For example:
export AIRFLOW_CONN_MSSQL_DEFAULT='mssql://username:password@server.com:1433/database_name'
If serializing with JSON:
export AIRFLOW_CONN_MSSQL_DEFAULT='{
"conn_type": "mssql",
"login": "username",
"password": "password",
"host": "server.com",
"port": 1433,
"schema": "database_name"
}'
Choosing the DBAPI driver¶
By default the hook connects with pymssql. Microsoft’s own
mssql-python driver is available as an alternative. Compared
with pymssql it supports Microsoft Entra ID authentication, and it uses Microsoft’s ODBC driver (installed
with it) instead of FreeTDS. Switching changes a few things, which are listed in the notes below. For
example, SQL written with %s placeholders has to be rewritten to use ?.
To use it, install the extra:
pip install 'apache-airflow-providers-microsoft-mssql[mssql-python]'
and set dbapi_driver to mssql_python in the connection extra. As a URI, the extras go in as query parameters:
export AIRFLOW_CONN_MSSQL_DEFAULT='mssql://username:password@example.com:1433/database_name?dbapi_driver=mssql_python&TrustServerCertificate=yes'
If serializing with JSON:
export AIRFLOW_CONN_MSSQL_DEFAULT='{
"conn_type": "mssql",
"login": "username",
"password": "password",
"host": "example.com",
"port": 1433,
"schema": "database_name",
"extra": {"dbapi_driver": "mssql_python", "TrustServerCertificate": "yes"}
}'
Notes¶
dbapi_driveracceptspymssql(the default) ormssql_pythonand is case-insensitive.Additional driver options, such as
Encrypt,TrustServerCertificateandAuthentication, can be supplied through connection extras.The default SQLAlchemy scheme is
mssql+mssqlpython, which requires SQLAlchemy 2.1 or newer. Override it usingsqlalchemy_schemein the connection extra or hook argument. The Airflow constraint files pin SQLAlchemy 2.0 and Airflow 2.11 requires it below 2.0, so on a constrained installget_conn()works, but methods that create a SQLAlchemy engine, such asget_sqlalchemy_engine(), fail withNoSuchModuleErroruntil SQLAlchemy is upgraded past the constraints.Use
?placeholders with positional parameters or%(name)swith named parameters. Plain%splaceholders are not supported. The hook defaults to?for generated statements.Driver-level connection pooling is enabled by default.
Connection extras are passed to the driver as connection string keywords.
get_conn()logs a warning and ignores keywords that mssql-python does not know, but the SQLAlchemy URL is rejected by the driver. Remove options that only pymssql understands, such ascharsetortds_version, when you switch.MsSqlToHiveOperatorandMSSQLToGCSOperatormap column types with pymssql type codes, so they need the defaultpymssqldriver for now.