@azure/postgresql-auth

中文文档
1.0.0发布于 2026/09/02MITTypeScriptESM + CJS近 30 天下载 1.7万

Azure Entra ID authentication extension for PostgreSQL in JavaScript

原始使用文档
文档目录12 节
  1. Getting started
  2. Currently supported environments
  3. Prerequisites
  4. Install the @azure/postgresql-auth package
  5. Key concepts
  6. Examples
  7. Using with node-postgres ( pg )
  8. Using with Sequelize
  9. Troubleshooting
  10. Logging
  11. Next steps
  12. Contributing

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.

版本与发布记录

@azure/postgresql-auth 共发布 2 个版本,首次发布于 2026/04/29;过去 12 个月发布了 2 个版本。

版本发布日期距今
1.0.0latest2026/09/022 个月前
1.0.0-beta.1beta2026/04/296 个月前

关于 @azure/postgresql-auth 的常见问题

@azure/postgresql-auth 是什么?

Azure Entra ID authentication extension for PostgreSQL in JavaScript

如何安装 @azure/postgresql-auth?

在项目目录运行 npm install @azure/postgresql-auth,或使用 pnpm add / yarn add / bun add。

@azure/postgresql-auth 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@azure/postgresql-auth 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@azure/postgresql-auth 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@azure/postgresql-auth 可以商用吗?

@azure/postgresql-auth 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@azure/postgresql-auth 的下载量有多少?

根据 npm 官方下载统计,@azure/postgresql-auth 在 2026/10/08 – 2026/11/06 这 30 天内被下载 16,676 次,日均约 556 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@azure/postgresql-auth 还在维护吗?

最新版本 1.0.0 发布于 2026/09/02(2 个月前);至今共发布 2 个版本,其中过去 12 个月发布了 2 个;首次发布于 2026/04/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。