How do I connect to SQL Server via sqlalchemy using Windows Authentication?

后端 未结 4 680
半阙折子戏
半阙折子戏 2020-12-05 03:57

sqlalchemy, a db connection module for Python, uses SQL Authentication (database-defined user accounts) by default. If you want to use your Windows (domain or local) credent

4条回答
  •  难免孤独
    2020-12-05 04:52

    A more recent response if you want to connect to the MSSQL DB from a different user than the one you're logged with on Windows. It works as well if you are connecting from a Linux machine with FreeTDS installed.

    The following worked for me from both Windows 10 and Ubuntu 18.04 using Python 3.6 & 3.7:

    import getpass
    from sqlalchemy import create_engine
    password = getpass.getpass()
    eng_str = fr'mssql+pymssql://{domain}\{username}:{password}@{hostip}/{db}'
    engine = create_engine(eng_str)
    

    What changed was to add the Windows domain before \username. You'll need to install the pymssql package.

提交回复
热议问题