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

58 lines
1.6 KiB
JavaScript

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* @summary Demonstrates how to connect to Azure Database for PostgreSQL using
* Entra ID authentication with the `pg` (node-postgres) library.
*/
const pg = require("pg");
const { DefaultAzureCredential } = require("@azure/identity");
const { entraTokenProvider } = require("@azure/postgresql-auth");
const dotenv = require("dotenv");
dotenv.config();
const { Pool } = pg;
async function main() {
const credential = new DefaultAzureCredential();
// Create a connection pool that uses Entra ID tokens as the password.
// The `password` option accepts a function that returns a Promise<string>,
// so the token is refreshed automatically on each new connection.
// `entraTokenProvider` returns such a function directly.
const pool = new 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 },
connectionTimeoutMillis: 20000,
idleTimeoutMillis: 30000,
});
try {
console.log("Connecting to Azure Database for PostgreSQL...");
const client = await pool.connect();
try {
const { rows } = await client.query(
"SELECT current_user, now() AS server_time, pg_backend_pid() AS backend_pid",
);
console.log("Connected successfully:", rows[0]);
} finally {
client.release();
}
} finally {
await pool.end();
console.log("Pool closed.");
}
}
main().catch((error) => {
console.error("Error:", error.message);
process.exit(1);
});