0

I am trying to connect to a SQL Server instance using SQLAlchemy through Python, however I require the SQL connection to come from a specific Windows AD account that isn't the one I run VSCode with. Is there any way to modify the connection string below to explicitly feed SQL Server a Windows login that isn't the same login I am using the run VSCode?

(I am able to connect with this connection string if I "Run as a different User" in VSCode, however the AD accounts with SQL access does not have shared drive access and therefore cannot access shared files, so this won't scale long-term)

import urllib
from sqlalchemy import create_engine

params = urllib.parse.quote_plus('DRIVER={SQL Server};SERVER={server};DATABASE={database};Trusted_Connection=Yes')

engine = create_engine(f'mssql+pyodbc:///?odbc_connect={params}')
  • 1
    I don't believe Python supports impersonation. It's an old [answer](https://stackoverflow.com/a/37702329/2029983) but it might help you, if you want to switch to using a different driver to pyodbc. Honestly, however, I recommend ensuring that the account you're using has permissions to do both "things". Otherwise you may have to look at SQL Authentication instead. – Thom A Jun 14 '22 at 13:48
  • 2
    You can't do anything with the connection string. You need to impersonate using the Windows functions such as `ImpersonateLoggedOnUser` – Charlieface Jun 14 '22 at 13:53
  • Makes sense to me, appreciate the help! – David Skrtich Jun 14 '22 at 13:54

0 Answers0