Files
0d69e49081 Port @azure/postgresql-auth package from postgres-entra-auth repo (#37647)
## Description

Migrates the standalone
[azure-postgresql-auth](https://github.com/Azure/postgres-entra-auth/tree/main/javascript)
library into the monorepo as `@azure/postgresql-auth` - a new Azure SDK
client package providing Entra ID authentication for PostgreSQL
connections via both `pg` (node-postgres) and Sequelize clients.

### Key changes:
- **Source:** TypeScript implementation with full type safety
- `getEntraTokenPassword()` - acquires Entra ID token for use as
PostgreSQL password
- `configureEntraIdAuth()` - registers Sequelize `beforeConnect` hook
for automatic token-based auth
- **Build:** Uses `warp` build system with 4 targets (browser,
react-native, esm, commonjs)
- **Tests:** 19 unit tests with vitest (all passing)
- **Peer deps:** `pg` (>=8.0.0) and `sequelize` (>=6.0.0) as optional
peer dependencies
- **CI:** New `sdk/postgresql/ci.yml` data-plane pipeline
- **CODEOWNERS:** Updated

### References
- Standalone source:
https://github.com/Azure/postgres-entra-auth/tree/main/javascript
- .NET reference: https://github.com/Azure/azure-sdk-for-net/pull/55231
## Release Plan Details
- Release Plan:
https://web.powerapps.com/apps/821ab569-ae60-420d-8264-d7b5d5ef734c?release-plan-id=0213048b-d1f0-f011-8407-000d3a3b8d97
- Work Item Link:
https://dev.azure.com/azure-sdk/fe81d705-3c06-41e5-bf7c-5ebea18efe89/_workitems/edit/30201
- Spec Pull Request: 
- Spec API version: 1.0.0

---------

Co-authored-by: Copilot <[email protected]>
Co-authored-by: Matthew Boentoro <[email protected]>
Co-authored-by: Jeremy Meng <[email protected]>
2026-04-13 11:39:26 -07:00

4.6 KiB

Azure PostgreSQL Authentication client library for JavaScript

This package provides Azure Entra ID (formerly Azure Active Directory) authentication for PostgreSQL connections in JavaScript. It supports both node-postgres (pg) and Sequelize clients.

Key links:

Getting started

Currently supported environments

See our support policy for more details.

Prerequisites

Install the @azure/postgresql-auth package

Install the Azure PostgreSQL Authentication client library for JavaScript with npm:

npm install @azure/postgresql-auth

You will also need to install @azure/identity for credential support and your preferred PostgreSQL client:

npm install @azure/identity pg
# or
npm install @azure/identity sequelize pg

Key concepts

This library provides two functions for integrating Entra ID authentication with PostgreSQL:

  • entraTokenProvider — Returns a password provider function that acquires an Entra ID access token suitable for use as a PostgreSQL password. Use this with pg.Pool or pg.Client.
  • configureEntraAuthentication — Registers a beforeConnect hook on a Sequelize instance that automatically acquires a fresh token and sets the username/password before each new connection.
  • GetEntraTokenPasswordOptions — Optionally override the default OAuth scope (scope property) passed to entraTokenProvider.
  • SequelizeBeforeConnectHook — Structural interface accepted by configureEntraAuthentication; lets you use any Sequelize-compatible object without adding a hard sequelize dependency.

Both functions accept an Azure TokenCredential (from @azure/identity) and handle token acquisition against the Azure Database for PostgreSQL scope.

Examples

Using with node-postgres (pg)

import { DefaultAzureCredential } from "@azure/identity";

const { entraTokenProvider } = await import("@azure/postgresql-auth");
const pg = await import("pg");
const credential = new DefaultAzureCredential();
const pool = new pg.Pool({
  host: process.env.PGHOST,
  port: Number(process.env.PGPORT || 5432),
  database: process.env.PGDATABASE,
  user: process.env.PGUSER,
  password: entraTokenProvider(credential),
  ssl: { rejectUnauthorized: true },
});

Using with Sequelize

import { DefaultAzureCredential } from "@azure/identity";

const { configureEntraAuthentication } = await import("@azure/postgresql-auth");
const { Sequelize } = await import("sequelize");
const sequelize = new Sequelize({
  dialect: "postgres",
  host: process.env.PGHOST,
  port: Number(process.env.PGPORT || 5432),
  database: process.env.PGDATABASE,
});
const credential = new DefaultAzureCredential();
configureEntraAuthentication(sequelize, credential);
await sequelize.authenticate();

Troubleshooting

Logging

Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the AZURE_LOG_LEVEL environment variable to info. Alternatively, logging can be enabled at runtime by calling setLogLevel in the @azure/logger:

import { setLogLevel } from "@azure/logger";

setLogLevel("info");

For more detailed instructions on how to enable logs, you can look at the @azure/logger package docs.

Next steps

Please take a look at the samples directory for detailed examples on how to use this library.

Contributing

If you'd like to contribute to this library, please read the contributing guide to learn more about how to build and test the code.