Package Exports
- @pgsql/parser
- @pgsql/parser/v13
- @pgsql/parser/v14
- @pgsql/parser/v15
- @pgsql/parser/v16
- @pgsql/parser/v17
Readme
@pgsql/parser
Multi-version PostgreSQL parser with dynamic version selection. This package provides a unified interface to parse PostgreSQL queries using different parser versions (13, 14, 15, 16, 17).
Installation
# Install latest (full build with all versions)
npm install @pgsql/parser
# Install LTS version (PostgreSQL 15-17 only)
npm install @pgsql/parser@ltsUsage
Dynamic Version Selection
import { parse, PgParser } from '@pgsql/parser';
// Parse with default version (17)
const result = await parse('SELECT 1+1 as sum');
console.log(result);
// { version: 17, result: { version: 170004, stmts: [...] } }
// Parse with specific version
const result15 = await parse('SELECT 1+1 as sum', 15);
console.log(result15);
// { version: 15, result: { version: 150007, stmts: [...] } }
// Using PgParser class
const parser = new PgParser(16);
const result16 = await parser.parse('SELECT * FROM users');Static Version Imports
For better tree-shaking and when you know which version you need:
// Import specific version
import * as pg17 from '@pgsql/parser/v17';
await pg17.loadModule();
const result = await pg17.parse('SELECT 1');
console.log(result);
// { version: 170004, stmts: [...] }Error Handling
The parser returns errors in a consistent format:
const result = await parse('INVALID SQL');
if (result.error) {
console.error(result.error);
// { type: 'syntax', message: 'syntax error at or near "INVALID"', position: 0 }
}API
parse(query: string, version?: 13 | 14 | 15 | 16 | 17): Promise<ParseResult>
Parse a SQL query with the specified PostgreSQL version.
query: The SQL query string to parseversion: PostgreSQL version (13, 14, 15, 16, or 17). Defaults to 17.
Returns a promise that resolves to:
- On success:
{ version: number, result: AST } - On error:
{ version: number, error: { type: string, message: string, position: number } }
PgParser
Class for creating a parser instance with a specific version.
const parser = new PgParser(version);
const result = await parser.parse(query);
const syncResult = parser.parseSync(query); // Only available after first parse()Version Exports
@pgsql/parser/v13- PostgreSQL 13 parser@pgsql/parser/v14- PostgreSQL 14 parser@pgsql/parser/v15- PostgreSQL 15 parser@pgsql/parser/v16- PostgreSQL 16 parser@pgsql/parser/v17- PostgreSQL 17 parser
Each version export provides:
loadModule(): Initialize the WASM moduleparse(query): Parse a query (async)parseSync(query): Parse a query (sync, requires loadModule first)
Build Configurations
This package supports different build configurations for different use cases:
- full (default): All versions (13, 14, 15, 16, 17) - Provides maximum compatibility
- lts: LTS (Long Term Support) versions only (15, 16, 17) - Recommended for production use with stable PostgreSQL versions
When installing from npm, you can choose the appropriate build using tags:
npm install @pgsql/parser- Full build with all versionsnpm install @pgsql/parser@lts- LTS build
Credits
Built on the excellent work of several contributors:
- Dan Lynch — official maintainer since 2018 and architect of the current implementation
- Lukas Fittl for libpg_query — the core PostgreSQL parser that powers this project
- Greg Richardson for AST guidance and pushing the transition to WASM and multiple PG runtimes for better interoperability
- Ethan Resnick for the original Node.js N-API bindings
- Zac McCormick for the foundational node-pg-query-native parser
Related
- pgsql-parser: The real PostgreSQL parser for Node.js, providing symmetric parsing and deparsing of SQL statements with actual PostgreSQL parser integration.
- pgsql-deparser: A streamlined tool designed for converting PostgreSQL ASTs back into SQL queries, focusing solely on deparser functionality to complement
pgsql-parser. - @pgsql/parser: Multi-version PostgreSQL parser with dynamic version selection at runtime, supporting PostgreSQL 15, 16, and 17 in a single package.
- @pgsql/types: Offers TypeScript type definitions for PostgreSQL AST nodes, facilitating type-safe construction, analysis, and manipulation of ASTs.
- @pgsql/enums: Provides TypeScript enum definitions for PostgreSQL constants, enabling type-safe usage of PostgreSQL enums and constants in your applications.
- @pgsql/utils: A comprehensive utility library for PostgreSQL, offering type-safe AST node creation and enum value conversions, simplifying the construction and manipulation of PostgreSQL ASTs.
- pg-proto-parser: A TypeScript tool that parses PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
- libpg-query: The real PostgreSQL parser exposed for Node.js, used primarily in
pgsql-parserfor parsing and deparsing SQL queries.
Disclaimer
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
No developer or entity involved in creating Software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the Software code or Software CLI, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.