pgsql-parser

The PostgreSQL Parser for Node.js

The real PostgreSQL parser for Node.js that interprets and converts PostgreSQL syntax. Uses actual PostgreSQL C parser via WebAssembly for symmetric parsing and deparsing with battle-tested reliability.

Real PostgreSQL Parser

Uses actual PostgreSQL C parser via WebAssembly

Symmetric Operations

Parse SQL to AST and deparse AST back to SQL

Battle-Tested

Tested with 23,000+ SQL statements

pgsql-parser Features

Professional-grade PostgreSQL parsing and AST manipulation

Authentic PostgreSQL Parser

Uses the actual PostgreSQL C parser via WebAssembly, ensuring 100% compatibility with PostgreSQL syntax.

Symmetric Parsing and Deparsing

Parse SQL statements to AST and convert AST back to SQL with perfect round-trip fidelity.

Battle-Tested Reliability

Extensively tested with over 23,000 SQL statements to ensure robust parsing of complex queries.

AST Manipulation

Programmatically modify, analyze, and transform Postgres queries through Abstract Syntax Tree manipulation.

WebAssembly Performance

Leverages WebAssembly for near-native performance while maintaining JavaScript accessibility.

TypeScript Support

Full TypeScript definitions for AST nodes, enabling type-safe query manipulation and analysis.

typescript
import { parse, deparse } from 'pgsql-parser';

// Parse SQL to AST
const stmts = await parse('SELECT * FROM test_table');

// Deparse AST back to SQL
const sql = await deparse(stmts);

Basic SQL Parsing and Deparsing

Parse SQL statements into AST and convert them back to SQL with perfect fidelity using the real Postgres parser.

typescript
import { parse, deparse } from 'pgsql-parser';

const stmts = await parse('SELECT * FROM test_table');

// Assuming the structure of stmts is known and matches the expected type
stmts[0].RawStmt.stmt.SelectStmt.fromClause[0].RangeVar.relname = 'another_table';

console.log(await deparse(stmts));

// SELECT * FROM "another_table"

AST Manipulation and Transformation

Modify SQL queries by manipulating their AST representation, enabling powerful query transformation capabilities.