Thanks a lot for the extra details and for confirming the host, dbname, token audience, and role flags – that rules out most of the usual connection-string issues.
At this point the error usually means that the Entra auth extension (pgaadauth) isn’t fully enabled or created on the server, so the auth hook fails before PostgreSQL has actually “landed” on a specific database, which gives the cannot read pg_class without having selected a database message.
Below is a step-by-step path to check and fix that.
#1. Double-check that Entra authentication is actually enabled on the server
In the Azure portal for your Flexible Server:
- Go to Authentication.
- Make sure one of these modes is selected:
- PostgreSQL and Microsoft Entra authentication, or
- Microsoft Entra authentication only
- Confirm that your Entra account (or group) is listed as a Microsoft Entra administrator and click Save if you changed anything.
When you enable Entra auth at the server level, the platform enables the PGAadAuth / pgaadauth extension and restarts the server behind the scenes.
If this blade still shows “PostgreSQL authentication only”, Entra auth isn’t active and the extension won’t be wired up.
#2. Check whether pgaadauth is available and allow-listed
Connect using your local PostgreSQL admin / azure_pg_admin with normal password auth (not Entra) and run:
SELECT name, default_version, installed_version
FROM pg_available_extensions
WHERE name = 'pgaadauth';
- If you get a row back, the extension is supported on this server.
- If no row is returned, the extension isn’t available on this build/region yet – for PostgreSQL 12–18 it should be available on Flexible Server, so in that case I’d recommend opening an Azure support ticket so the product team can check your instance.
If pgaadauth is available but not allow-listed:
- In the portal, go to Server parameters.
- Search for
azure.extensions.
- Add
pgaadauth to the comma-separated list of extensions (for example: pg_stat_statements,pgaudit,pgaadauth).
- Save the change (the server may need a restart).
This is the standard way to allow extensions such as pgaadauth on Flexible Server.
#3. Create the pgaadauth extension (once allow-listed)
Still connected as azure_pg_admin (or equivalent), usually in the postgres database:
CREATE EXTENSION IF NOT EXISTS pgaadauth;
If this fails, note the exact error text – that will tell us whether it is a permissions problem or an allow-listing problem.
#4. Create the Entra principal inside the database
Once the extension exists, you can use the helper function to create the database role that matches your Entra identity or group:
-- Replace with your UPN or group display name
SELECT *
FROM pg_catalog.pgaadauth_create_principal(
'<UPN or Group Display Name>',
false, -- is_group
false -- create_if_not_exists_only
);
GRANT CONNECT ON DATABASE "<confirmed_dbname>"
TO "<UPN or Group Display Name>";
GRANT USAGE ON SCHEMA public
TO "<UPN or Group Display Name>";
You can verify the principal with:
SELECT *
FROM pg_catalog.pgaadauth_list_principals(false);
:contentReference[oaicite:3]{index=3}
This step is required because being an Entra administrator at the server level doesn’t automatically create per-database roles.
#5. Re-test the psql connection with the token
From Cloud Shell (your commands look correct, but just to have the full pattern in one place):
# 1. Get token
az account get-access-token --resource-type oss-rdbms --query accessToken -o tsv > token.txt
# 2. Use that token as PGPASSWORD
export PGPASSWORD=$(cat token.txt)
# 3. Connect using FQDN, exact UPN, and database name
psql "host=<your_server>.postgres.database.azure.com \
port=5432 \
user=<your_upn_or_group_name> \
dbname=<confirmed_dbname> \
sslmode=require"
Key points:
- The
user value must exactly match the UPN (or group display name) you passed to pgaadauth_create_principal.
- The token’s
aud should remain https://ossrdbms-aad.database.windows.net and upn/oid should map to that principal, which you’ve already confirmed.
If it still fails If after:
- Enabling Entra auth on the server,
- Allow-listing and creating
pgaadauth,
- Creating the principal via
pgaadauth_create_principal, and
- Connecting with the exact UPN +
dbname,
If you still encounter FATAL: cannot read pg_class without having selected a database, it’s very likely a platform-side issue related to the Entra authentication hook on this specific server build—especially since you’re on the PostgreSQL 18 preview. We’ll escalate this to the next level of support.