skillfed

pglast

PostgreSQL Languages AST and statements prettifier

pglast v8.4 4.8M downloads/30d#2,220 on PyPI402
Copyleft license GPL-3.0-or-later Active released

What it is and what it does

pglast is a Python module that wraps the PostgreSQL parser (via libpg_query) to expose SQL statements as an abstract syntax tree. It lets you programmatically inspect the structure of SQL queries—their clauses, expressions, and relationships—rather than treating them as opaque strings. The tree is built from interconnected nodes that mirror PostgreSQL's internal parse representation.

The package is typically used to analyze, validate, or transform SQL statements without executing them. Common tasks include extracting table and column references, detecting query patterns, reformatting SQL for readability, or building tools that need to understand query structure. It has no runtime dependencies and covers modern Python versions; installation requires compiled wheels for your platform.

Use it for:

  • Extract table and column names from arbitrary SQL queries for dependency analysis or schema mapping
  • Reformat or prettify SQL statements to a consistent style for code review or documentation
  • Build linters or validators that check SQL queries for anti-patterns or compliance rules
  • Analyze query structure to optimize or rewrite statements programmatically
  • Generate documentation or diagrams of data lineage from SQL statement trees

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Parses PostgreSQL SQL statements into an abstract syntax tree and provides tools to inspect and prettify the parsed structure.

Yes, if you need to parse and inspect PostgreSQL SQL statements programmatically. The package is actively maintained, has no runtime dependencies, and covers modern Python versions. The GPL-3.0-or-later license is a blocker only if you cannot open-source derivative works; otherwise, it's a solid choice for SQL analysis tasks.

Install

pglast on PyPI

pip

pip install pglast

uv

uv add pglast

poetry

poetry add pglast

Installing pglast

Before you install

Medium install friction due to compiled wheels for multiple platforms and Python versions (3.10–3.14). Active maintenance with recent release 23 days ago and 402 repository stars suggests stable, ongoing support.

License in practice

Licensed under GPL-3.0-or-later (copyleft). Use in proprietary software requires careful review of your licensing obligations, as derivative works must also be open-source under compatible terms.

Quickstart

pip install pglast

import pglast

ast = pglast.parse('SELECT * FROM table')
print(pglast.prettify(ast))

Verify before relying

  • Minimum Python version requirement (classifiers list 3.10–3.14 but requires_python is unspecified)
  • Whether the package works with all PostgreSQL versions or requires a specific server version
  • Performance characteristics when parsing very large or complex SQL statements

Package facts

License GPL-3.0-or-later (copyleft)
Python support not specified
Install friction medium — platform-specific wheel
Runtime dependencies none
Maintenance actively maintained — 23 days since the last release
Last repo commit
First released
Downloads 4,822,899/month — #2,220 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: pglast-8.4-cp310-cp310-macosx_10_9_x86_64.whl; pglast-8.4-cp310-cp310-macosx_11_0_arm64.whl; pglast-8.4-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl; pglast-8.4-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl; pglast-8.4-cp310-cp310-musllinux_1_2_aarch64.whl; pglast-8.4-cp310-cp310-musllinux_1_2_x86_64.whl; pglast-8.4-cp310-cp310-win32.whl; pglast-8.4-cp310-cp310-win_amd64.whl; pglast-8.4-cp311-cp311-macosx_10_9_x86_64.whl; pglast-8.4-cp311-cp311-macosx_11_0_arm64.whl; pglast-8.4-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl; pglast-8.4-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl; pglast-8.4-cp311-cp311-musllinux_1_2_aarch64.whl; pglast-8.4-cp311-cp311-musllinux_1_2_x86_64.whl; pglast-8.4-cp311-cp311-win32.whl; pglast-8.4-cp311-cp311-win_amd64.whl; pglast-8.4-cp312-cp312-macosx_10_13_x86_64.whl; pglast-8.4-cp312-cp312-macosx_11_0_arm64.whl; pglast-8.4-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl; pglast-8.4-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl

Keywords: postgresql, parser, sql, prettifier

Development Status :: 5 - Production/StableIntended Audience :: DevelopersProgramming Language :: PythonProgramming Language :: Python :: 3Programming Language :: Python :: 3 :: OnlyProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Programming Language :: SQLTopic :: DatabaseTopic :: Utilities

Tags

postgresql sql parserparse sql statementssql ast treepostgresql query analysissql prettifierpostgres statement parsersql syntax tree
sql-parsingpostgres-toolsast-analysis

More Utilities packages