I would want to connect SqlAlchemy to Azure Database for Postgres with token based authentication to achieve Single Sign On (SSO) by using Azure Active Directory (AAD), but seems like SqlAlchemy poorly support token-based connection string, is there any way to achieve it? In my solution, SqlAlchemy is required, and Single-Sign-On with Azure AD is required, and no password provided.
I browsed and it seems like pyodbc library work with token auth, pyodbc lib token auth, but I need SqlAlchemy, it's very cutting-edge case, I believe.
You can follow the procedure below to connect to an Azure Database for PostgreSQL server using access token authentication:
Use the code below to get the access token for authentication:
Use the code below to connect to the Azure Database for PostgreSQL server using SQLAlchemy:
Below is the complete code:
You will get the output as mentioned below: