# pg-introspection

> Strongly typed PostgreSQL introspection library

Latest version **1.0.2** (published 2026-10-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install pg-introspection
pnpm add pg-introspection
yarn add pg-introspection
bun add pg-introspection
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; no vulnerabilities; has provenance; recently updated; high maintenance score; popular repo.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2026-10-30 |
| First published | 2022-09-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=22 |
| Dependencies | 1 |
| Unpacked size | 207.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 12934 |
| Author | Benjie Gillam |
| Maintainers | benjie |
| Keywords | pg, postgres, postgresql, introspection, pg_class, pg_constraint, pg_attribute, pg_proc, graphile, graphite |

## Links

- npm: https://www.npmjs.com/package/pg-introspection
- Repository: https://github.com/graphile/crystal
- Homepage: https://github.com/graphile/crystal/tree/main/utils/pg-introspection
- Issues: https://github.com/graphile/crystal/issues
- npm.io page: https://npm.io/package/pg-introspection

## Dependencies (1)

- [tslib](https://npm.io/package/tslib.md) ^2.8.1

## Alternatives

- [d3-force-3d](https://npm.io/package/d3-force-3d.md) — 1.0M weekly downloads
- [ng2-charts](https://npm.io/package/ng2-charts.md) — 486.8K weekly downloads
- [@arcgis/core](https://npm.io/package/@arcgis/core.md) — 257.8K weekly downloads
- [react-sparklines](https://npm.io/package/react-sparklines.md) — 249.3K weekly downloads
- [react-native-gifted-charts](https://npm.io/package/react-native-gifted-charts.md) — 182.3K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2026-10-30
- 1.0.2-aardvark.next-20260928202932 (snapshot-next) — 2026-09-28
- 1.0.0-rc.5 (rc) — 2026-03-10
- 0.0.1-beta.13 (beta) — 2025-09-24
- 0.0.1-alpha.4 (alpha) — 2023-07-13
- 0.0.1-alpha.1 (prealpha) — 2023-04-26
- 1.0.2-aardvark.next-20260925164249 — 2026-09-25
- 1.0.2-aardvark.next-20260922224416 — 2026-09-22
- 1.0.2-aardvark.next-20260919185130 — 2026-09-19
- 1.0.2-aardvark.next-20260918085327 — 2026-09-18
- 1.0.2-aardvark.next-20260910230054 — 2026-09-10
- 1.0.2-aardvark.next-20260907131448 — 2026-09-07
- 1.0.2-aardvark.next-20260902182429 — 2026-09-02
- 1.0.2-aardvark.next-20260902153400 — 2026-09-02
- 1.0.2-aardvark.next-20260901113320 — 2026-09-01
- … 35 more at https://npm.io/package/pg-introspection/versions

## README

# pg-introspection

[![GitHub Sponsors](https://img.shields.io/github/sponsors/benjie?color=ff69b4&label=github%20sponsors)](https://github.com/sponsors/benjie)
[![Discord chat room](https://img.shields.io/discord/489127045289476126.svg)](http://discord.gg/graphile)
[![Follow](https://img.shields.io/badge/BSky-@Graphile.org-006aff.svg)](https://bsky.app/profile/graphile.org)
[![Follow](https://img.shields.io/badge/Mastodon-@Graphile.fosstodon.org-6364ff.svg)](https://fosstodon.org/@graphile)

A strongly-typed PostgreSQL introspection library for PostgreSQL built
automatically from the
[PostgreSQL system catalog documentation](https://www.postgresql.org/docs/current/catalogs.html),
with the TypeScript documentation for each attribute/type also pulled from the
PostgreSQL documentation for easy reference directly in your editor.

<!-- SPONSORS_BEGIN -->

## Crowd-funded open-source software

To help us develop this software sustainably, we ask all individuals and
businesses that use it to help support its ongoing maintenance and development
via sponsorship.

### [Click here to find out more about sponsors and sponsorship.](https://www.graphile.org/sponsor/)

And please give some love to our featured sponsors 🤩:

<table><tr>
<td align="center"><a href="https://gosteelhead.com/"><img src="https://graphile.org/images/sponsors/steelhead.svg" width="90" height="90" alt="Steelhead" /><br />Steelhead</a> *</td>
</tr></table>

<em>\* Sponsors the entire Graphile suite</em>

<!-- SPONSORS_END -->

## Usage

Issue the `makeIntrospectionQuery()` SQL query to your database, then feed the
first row's `introspection` field into `parseIntrospectionResults()` to get your
strongly typed introspection results.

Example usage with `pg` module:

```js
import {
  makeIntrospectionQuery,
  parseIntrospectionResults,
} from "pg-introspection";
import { Pool } from "pg";

const pool = new Pool({
  connectionString: "postgres://localhost:5432/my_database",
});

async function main() {
  const sql = makeIntrospectionQuery();
  const { rows } = await pool.query(sql);
  const introspection = parseIntrospectionResults(rows[0].introspection);

  console.log(
    `The ${introspection.database.datname} DBA is ${
      introspection.database.getDba()?.rolname ?? "-"
    }`,
  );
}

main();
```

## Accessors

Into the introspection results we mix "accessor" functions to make following
relationships easier. Note that these functions are typically evaluated lazily -
the first time you call them they may need to do an expensive lookup (e.g.
finding the relevant record from the list of records) but they cache the result
so that the next call will be near-instant.

Examples:

```js
const myTable = introspection.classes.find((rel) => rel.relname === "my_table");
const myTableAttributes = myTable.getAttributes();
const myColumn = myTable.getAttribute({ name: "my_column" });
const myColumnDescription = myColumn.getDescription();
```

You can use the TypeScript autocompletion to see what accessors are available,
or look in the `index.ts` file.

## Naming

Using the PostgreSQL column names is _by design_, even though some are hard to
read if you're not familiar with the system catalog.

We use `_id` rather than `oid` because older versions of PostgreSQL did not
explicitly list the `oid` columns when you `select * from` so we explicitly list
them among the selection set.

---
_Source: https://npm.io/package/pg-introspection · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
