postgast

GitHub | PyPI

BSD-licensed Python bindings to libpg_query, the PostgreSQL parser extracted as a standalone C library.

Parse, deparse, normalize, fingerprint, split, and scan PostgreSQL SQL statements from Python with a minimal dependency footprint — just protobuf and the vendored C library.

import postgast

# Parse a query into an AST
tree = postgast.parse("SELECT id, name FROM users WHERE active = true")

# Deparse an AST back to SQL
sql = postgast.deparse(tree)

# Normalize a query (replace constants with placeholders)
postgast.normalize("SELECT * FROM users WHERE id = 42")
# => "SELECT * FROM users WHERE id = $1"

# Fingerprint a query
fp = postgast.fingerprint("SELECT * FROM users WHERE id = 42")

# Split a multi-statement string
postgast.split("SELECT 1; SELECT 2;")
# => ["SELECT 1", "SELECT 2"]

Installation

pip install postgast

LLM-Friendly Docs

Machine-readable documentation is available for AI coding assistants:

These files follow the llms.txt standard.