skip to Main Content

I am having a mssql server running in docker, I want to connect the server using sqlalchemy

For connecting, I am using this connection string,

engine = create_engine(
    'mssql+pyodbc:///?odbc_connect=%s' % (
        urllib.parse.quote_plus(
            'DRIVER={/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so};SERVER=localhost;'
            'DATABASE=ap;UID=sa;PWD=admin@123;port=1433;'
            )), 
)

For this, I got this error,

sqlalchemy.exc.DBAPIError: (pyodbc.Error) (‘01000’, "[01000] [unixODBC][Driver Manager]Can’t open lib ‘/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so’ : file not found (0) (SQLDriverConnect)")

Then, I figured that this file is missing. How to get libtdsodbc.so file

I am using Ubuntu 22.04.2 LTS

the docker mssql server is 2017,

in docker status, the port is shown as 0.0.0.0:1433->1433/tcp, :::1433->1433/tcp

Thanks

2

Answers


  1. You can install the Microsoft ODBC Driver for SQL Server on Linux by following the instructions here:

    if ! [[ "18.04 20.04 22.04" == *"$(lsb_release -rs)"* ]];
    then
        echo "Ubuntu $(lsb_release -rs) is not currently supported.";
        exit;
    fi
    
    sudo su
    curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
    
    curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list > /etc/apt/sources.list.d/mssql-release.list
    
    exit
    sudo apt-get update
    sudo ACCEPT_EULA=Y apt-get install -y msodbcsql18
    # optional: for bcp and sqlcmd
    sudo ACCEPT_EULA=Y apt-get install -y mssql-tools18
    echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bashrc
    source ~/.bashrc
    # optional: for unixODBC development headers
    sudo apt-get install -y unixodbc-dev
    

    Install the Microsoft ODBC driver for SQL Server (Linux)

    Login or Signup to reply.
  2. You can install libtdsodbc.so using

    apt update
    apt install tdsodbc
    

    although the Ubuntu 22.04 repo installs a rather old version, 1.3.6 (released 17 months ago), whereas the most recent stable release is currently 1.3.18 (released 7 weeks ago).

    Also, SQLAlchemy is not tested with FreeTDS ODBC for the mssql+pyodbc:// dialect, so you may be happier in the long run if you use Microsoft’s ODBC driver for SQL Server as suggested in the other answer.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search