I'm trying to connect to a MSSQL database; the issue is that on the network where this script is ran, we use a self-signed certificate internally and I think pyodbc is not happy with that.
The database I am connecting to (MSSQL) lives outside our network - and when I try to connect to the database by running my script on a server (Windows 2016), I get the following errors:
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]SSL Provider: The certificate chain was issued by an authority that is not trusted.
I'm not sure how to get pyodbc to recognize that our self-signed cert is 'to be trusted'.
The connection string looks like this:
def get_db_cursor():
driver = os.environ.get("ODBC_DRIVER") # ODBC Driver 17 for SQL Server
host = os.environ.get("db_HOST")
db_name = os.environ.get("db_NAME")
db_user = os.environ.get("db_USER")
db_pass = os.environ.get("db_PASSWORD")
db_port = os.environ.get("db_PORT")
connection = pyodbc.connect(
"DRIVER="
+ driver
+ ";SERVER="
+ host
+ ";PORT="
+ db_port
+ ";DATABASE="
+ db_name
+ ";UID="
+ db_user
+ ";PWD="
+ db_pass
+ ";Encrypt=yes;"
)
db_cursor = connection.cursor()
return db_cursor
I use Encrypt=yes
because it's leaving our network and I'd like it encrypted.
I can use TrustServerCertificate=True
, but that is not the correct/safe way to do it (but it would work).
I am curious if anyone knows of or has any other ideas on how to get pyodbc to accept a self-signed cert?
Maybe I'm going about this all wrong - but I just can't seem to get this to not throw that error. All help is appreciated.