[Python] SQL Server Connection Issue from My Flask Application hosted on Render

Discussão em 'Python' iniciado por Stack, Fevereiro 8, 2025.

  1. Stack

    Stack Membro Participativo

    I'm had make Flask Server with SQL server and I had hosted my flask application on Render . I'm able to connect to my database from my local system but when I host it on Render I'm not able to connect and getting this error . Also My database is hosted on AWS .


    Error :::: { "message": "Failed to connect to the database: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)")", "status": "error" }


    def get_db_connection():
    try:
    conn = pyodbc.connect(
    f"DRIVER={{ODBC Driver 17 for SQL Server}};"
    f"SERVER={DB_SERVER},1433;"
    f"DATABASE={DB_NAME};"
    f"UID={DB_USER};"
    f"PWD={DB_PASSWORD};"
    )
    print("Connected to the database!")
    return conn
    except Exception as e:
    print(f"Database connection failed: {e}")
    raise e


    Working properly on local system


    Dockerfile

    FROM python:3.13.0-buster

    # UPDATE APT-GET
    RUN apt-get update

    # PYODBC DEPENDENCES
    RUN apt-get install -y tdsodbc unixodbc-dev
    RUN apt install unixodbc-bin -y
    RUN apt-get clean -y
    ADD odbcinst.ini /etc/odbcinst.ini

    # UPGRADE pip3
    RUN pip3 install --upgrade pip

    # DEPENDECES FOR DOWNLOAD ODBC DRIVER
    RUN apt-get install apt-transport-https
    RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
    RUN curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/mssql-release.list
    RUN apt-get update

    # INSTALL ODBC DRIVER
    RUN ACCEPT_EULA=Y apt-get install msodbcsql17 --assume-yes

    # CONFIGURE ENV FOR /bin/bash TO USE MSODBCSQL17
    RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
    RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc

    Continue reading...

Compartilhe esta Página