Postgres AST Toolkit

Babel for Postgres: parse SQL and PL/pgSQL into syntax trees, traverse and transform nodes, lint source, and write SQL back out. Companion tools test queries and access policies against a real database.

Packages and tools

  • libpg-query

    Constructive’s WebAssembly build of the PostgreSQL parser for JavaScript. Parse SQL with the real PostgreSQL grammar without compiling a native add-on. The older @libpg-query/parser package is retired.

  • pgsql-parser

    Parse PostgreSQL SQL into a typed syntax tree with the real PostgreSQL parser in WebAssembly, then inspect, transform, or serialize it back to SQL from JavaScript or TypeScript.

  • pgsql-deparser

    Turn PostgreSQL syntax trees back into SQL using TypeScript, without a native add-on or a WebAssembly runtime. Use it to generate SQL from trees you build or transform.

  • pgsql-parse

    Parse and rewrite SQL while preserving comments and whitespace on unchanged text.

  • plpgsql-parser

    Parse SQL and PL/pgSQL function bodies into syntax trees and transform them together.

  • plpgsql-deparser

    Turn PL/pgSQL procedural syntax trees back into source text.

  • @pgsql/parser

    Select a PostgreSQL parser major version at runtime.

  • @pgsql/types

    TypeScript definitions for PostgreSQL parser nodes, published by PostgreSQL major.

  • @pgsql/enums

    Enum values and member names from the PostgreSQL AST, published by PostgreSQL major.

  • @pgsql/quotes

    Quote PostgreSQL identifiers and classify keywords when generating SQL.

  • @pgsql/traverse

    Walk SQL and PL/pgSQL trees to inspect and transform nodes.

  • @pgsql/lint

    Check SQL and PL/pgSQL source for conventions such as schema qualification without connecting to a database.

  • @pgsql/utils

    Construct PostgreSQL syntax-tree nodes in TypeScript.

  • @pgsql/semantics

    Analyze PostgreSQL syntax trees for semantic information beyond their raw node shapes.

  • @pgsql/transform

    Apply transformations to PostgreSQL SQL source.

  • pg-proto-parser

    Generate TypeScript definitions and enums from PostgreSQL parser protobuf files.

  • Postgres Testing Toolkit

    Test against a real PostgreSQL database with seeded fixtures, per-test rollback, and role or JWT context for row-level security. Includes adapters for Supabase and Drizzle.

Verified usage

  • Supabase Studio

    The parse-query API uses libpg-query to identify SQL operations, tables, and WHERE clauses.

    View source
  • Supabase Postgres Meta

    The parser service calls pgsql-parser to turn SQL into a syntax tree and serialize it back to SQL.

    View source
  • Supabase pg-topo

    The pg-toolbelt ingestion code uses plpgsql-parser to parse SQL and reconstruct a statement when its original source slice cannot be reused.

    View source
  • AWS Advanced Node.js Wrapper · ESLint rule

    AWS uses libpg-query to help prevent the kind of SQL hijacking behind past driver CVEs. Before shipping the Advanced Node.js Wrapper, its ESLint rule calls loadModule and parseSync to check internal SQL with PostgreSQL’s own grammar. In queries it can parse, it flags functions, operators, and cast types without a schema so maintainers can fix references that search_path could resolve to the wrong object.

    View source
  • Neon semicolons · test suite

    The tests compare the SQL splitter’s output to pgsql-parser. This is test usage, not a runtime dependency.

    View source

Related Blog Posts

The Babel for Postgres: How AWS Lints SQL in Its Node.js Wrapper

How the AWS Advanced Node.js Wrapper uses the libpg-query npm package in a development-time ESLint rule to check SQL qualification.