- pglast (uses
libpg_query) - sqlglot (pure Python)
- sqlparse (pure Python)
- sqlparser (NOT MAINTAINED ANYMORE)
- psqlparse (NOT MAINTAINED ANYMORE, uses
libpg_query)
Using sqlparse:
Create the file sqlfmt.py:
# pip install sqlparse
import sys
import sqlparse
sql = sys.stdin.read()
statements = sqlparse.split(sql)
for statement in statements:
formatted = sqlparse.format(statement, reindent=True, keyword_case="upper")
print(f"{formatted}\n")Usage:
cat file.sql | python sqlfmt.py