I’m deploying a Python application using FastAPI, SQLAlchemy, and pyodbc to Render, and I’m facing the following error when trying to connect to my SQL Server database:
(pyodbc.Error) ('0A000', '[0A000] [Microsoft][ODBC Driver 18 for SQL Server]SSL Provider: [error:0A00014D:SSL routines::invalid version number] (-1) (SQLDriverConnect)')
I’m using Docker with the msodbcsql18 driver installed. Here’s my connection setup:
params = urllib.parse.quote_plus(
f"DRIVER={{ODBC Driver 18 for SQL Server}};"
f"SERVER={SERVER},{PORT};"
f"DATABASE={DATABASE};"
f"UID={USERNAME};"
f"PWD={PASSWORD};"
f"TrustServerCertificate=yes;"
"Encrypt=no"
)
connection_string = f"mssql+pyodbc:///?odbc_connect={params}"
engine = create_engine(connection_string)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
I also tried using "Encrypt=yes" and "MinTLSVersion=1.0", but the error still occurs.
This is my Dockerfile:
FROM python:3.11-slim
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV OPENSSL_CONF=/etc/ssl/openssl.cnf
COPY openssl.cnf /etc/ssl/openssl.cnf
RUN apt-get update && apt-get install -y \
curl gnupg apt-transport-https unixodbc unixodbc-dev gcc g++ \
&& curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | tee /etc/apt/trusted.gpg.d/microsoft.gpg > /dev/null \
&& echo "deb [arch=amd64 signed-by=/etc/apt/trusted.gpg.d/microsoft.gpg] https://packages.microsoft.com/debian/12/prod bookworm main" > /etc/apt/sources.list.d/mssql-release.list \
&& apt-get update && ACCEPT_EULA=Y apt-get install -y msodbcsql18 \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY requirements.txt ./
RUN pip install --upgrade pip && pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["gunicorn", "app:app", "--bind", "0.0.0.0:5000"]