A Python app on Azure must connect to Azure Database for PostgreSQL flexible server without storing a database password. What is the recommended approach?
- Exam domain
- Develop AI solutions by using Azure data management services25–30% of the exam
- Difficulty
- Medium
- In this mock
- 18 questions on this domainPractise this domain
Why the exam asks this
Objective in the exam guide: Connect and query Azure Database for PostgreSQL by using SDKs.
Microsoft recommends passwordless connections for PostgreSQL. The exam checks how the token replaces the password.
Why B is the answer
Use Microsoft Entra authentication: get a token with DefaultAzureCredential and pass it as the password when connecting with psycopg
With Microsoft Entra authentication, the app gets an access token for its identity and uses it in place of a password, so no password is stored.
Remember: Passwordless PostgreSQL: Entra token as the password.
How it works
Azure Database for PostgreSQL supports Microsoft Entra authentication for users, groups, service principals and managed identities.
The app gets a token with DefaultAzureCredential and passes it as the password in the psycopg connection.
Microsoft labels this passwordless approach as recommended in its Python quickstart.
Clues in the question
The words that point to the answer, and what each one tells you.
- Without storing a database password
- Entra token authentication.
In detail
- The identity must be added as a Microsoft Entra user on the server.
- Tokens are short-lived, so refresh them when opening new connections or use a library that does it.
Side by side with look-alikes
| Method | Stored secret? |
|---|---|
| Entra token via DefaultAzureCredential | No |
| Password in environment variable | Yes |
| Admin password in code | Yes |
Every option, checked
Knowing why each wrong option fails is how you rule it out on exam day.
A. Use the server's public IP address instead of its host nameWrong
The address does not change authentication.
B. Use Microsoft Entra authentication: get a token with DefaultAzureCredential and pass it as the password when connecting with psycopgRight
Passwordless PostgreSQL: Entra token as the password.
C. Store the admin password in an environment variableWrongMost tempting
An environment variable still holds a stored password.
Why it tempts: Environment variables keep passwords out of code, which sounds secure. But the password is still stored, and it is the admin account.
D. Disable SSL so no credentials are neededWrong
Turning off SSL weakens security and still needs credentials.
In practice
- Grant the identity only the database roles it needs, not admin.
Use case
An illustrative scenario, not a real incident.
A web app on App Service uses its managed identity to connect to PostgreSQL; no password exists anywhere.
See how it works
sequenceDiagram participant A as App (managed identity) participant E as Microsoft Entra ID participant P as PostgreSQL flexible server A->>E: Get token for PostgreSQL E-->>A: Access token A->>P: Connect: user = identity name, password = token P-->>A: Session
How to read this chart
- 1. App (managed identity) sends "Get token for PostgreSQL" to Microsoft Entra ID.
- 2. App (managed identity) gets a reply from Microsoft Entra ID: "Access token".
- 3. App (managed identity) sends "Connect: user = identity name, password = token" to PostgreSQL flexible server.
- 4. App (managed identity) gets a reply from PostgreSQL flexible server: "Session".