This document explains how to connect to Azure Database for PostgreSQL Flexible Server using Microsoft Entra Authentication from client tools such as pgAdmin and Visual Studio Code (PostgreSQL Extension).
The authentication approach differs slightly depending on whether:
- An individual Entra User is configured as an administrator.
- An Entra Group is configured as an administrator.
Understanding this distinction is critical, especially when connecting through tools such as pgAdmin that require manual token-based authentication.
Before proceeding, ensure:
- Microsoft Entra Authentication is enabled for the PostgreSQL Flexible Server.
- Your user account is a member of the configured Entra administrator account or group.
- Azure PowerShell (
Azmodule) or Azure CLI is installed. - You have network connectivity to the PostgreSQL server.
- pgAdmin or VS Code PostgreSQL extension is installed.
Administrator configured in Azure
abc@abc.com
Username : abc@abc.com
Password : Access Token generated for abc@abc.com
User (abc@abc.com)
|
v
Generate Access Token
|
v
pgAdmin Login
Username = abc@abc.com
Password = Access Token
|
v
Azure PostgreSQL
Connect-AzAccountGet-AzContext$tokenSecure = (Get-AzAccessToken -ResourceUrl "https://ossrdbms-aad.database.windows.net").Token
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($tokenSecure)
$token = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
$tokenUsername : abc@abc.com
Password : <Access Token>
The connection should be established successfully.
Administrator configured in Azure
PG-DBA-Admins
User
abc@abc.com
Member Of
PG-DBA-Admins
When an Entra Group is configured as PostgreSQL Administrator:
- The access token is still generated using the individual user account.
- The username supplied during login must be the Entra Group Name, not the individual user account.
Username : PG-DBA-Admins
Password : Access Token generated for abc@abc.com
User (abc@abc.com)
|
v
Generate Access Token
|
v
pgAdmin Login
Username = PG-DBA-Admins
Password = User Access Token
|
v
Azure validates:
- User token
- User membership in group
|
v
Access Granted
Using:
Username : abc@abc.com
Password : <Access Token>
When the PostgreSQL administrator is configured as:
PG-DBA-Admins
This can result in authentication failure.
Username : PG-DBA-Admins
Password : <Access Token generated for abc@abc.com>
# Authenticate to Azure
Connect-AzAccount
# Verify active context
Get-AzContext
# Obtain PostgreSQL access token
$tokenSecure = (Get-AzAccessToken -ResourceUrl "https://ossrdbms-aad.database.windows.net").Token
# Convert SecureString to plain text
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($tokenSecure)
$token = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
# Display token
$tokenCopy the generated token and use it as the password in pgAdmin.
az loginaz account get-access-token \
--resource-type oss-rdbms \
--query accessToken \
-o tsvAlternatively:
az account get-access-token \
--resource https://ossrdbms-aad.database.windows.net \
--query accessToken \
-o tsvCopy the token output and use it as the password in pgAdmin.
Name : Azure PostgreSQL
Host Name : <server-name>.postgres.database.azure.com
Port : 5432
Database : postgres
(or target database)
Username :
- Entra User Name
OR
- Entra Group Name
Password :
Access Token
SSL Mode : Require
The PostgreSQL extension for VS Code provides a more user-friendly authentication experience.
- Install the PostgreSQL extension.
- Create a new PostgreSQL connection.
- Select:
Authentication Type = Microsoft Entra ID
- A browser authentication window will appear.
- Sign in using your Entra account.
- Complete MFA if prompted.
- The extension automatically retrieves and manages the access token.
- No manual token generation required.
- No need to paste access tokens into the password field.
- Supports interactive browser-based sign-in.
- Handles token refresh automatically.
Verify the following:
- User is configured as PostgreSQL administrator, or
- User is a member of the configured Entra administrator group.
- The correct username is being used.
- A fresh access token is supplied.
Access tokens have a limited lifetime.
Generate a new token and reconnect.
Verify:
- User is a member of the Entra group.
- Entra group is configured as PostgreSQL administrator.
- Group name is specified in the Username field.
- Access token belongs to a valid member of the group.
Administrator = abc@abc.com
Username = abc@abc.com
Password = User Access Token
Administrator = PG-DBA-Admins
Username = PG-DBA-Admins
Password = Access Token generated for abc@abc.com
When an individual Entra user is configured as the PostgreSQL administrator:
Username = User UPN
Password = User Access Token
When an Entra Group is configured as the PostgreSQL administrator:
Username = Entra Group Name
Password = Access Token generated for the individual user
This distinction is the most common reason for Microsoft Entra authentication failures in pgAdmin and other PostgreSQL client utilities.