A PostgreSQL SQL formatter
Project description
pgfmt
A PostgreSQL SQL formatter with multiple style options.
pgfmt parses SQL using pgparse (PostgreSQL's own parser via libpg_query) and reformats it according to one of several well-known style guides.
Installation
pip install pgfmt
CLI Usage
# Format a file (default: river style)
pgfmt query.sql
# Format from stdin
echo "SELECT a,b FROM t WHERE x=1" | pgfmt
# Choose a style
pgfmt --style mozilla query.sql
pgfmt --style dbt query.sql
# Check if already formatted (exit 1 if not)
pgfmt --check query.sql
Library Usage
import pgfmt
sql = "SELECT a, b FROM my_table WHERE x = 1 AND y = 2"
# Default (river) style
print(pgfmt.format(sql))
# Choose a style
print(pgfmt.format(sql, style='mozilla'))
print(pgfmt.format(sql, style='dbt'))
Styles
river (default)
Based on sqlstyle.guide by Simon Holywell. Keywords are right-aligned to form a visual "river" separating keywords from content. Uppercase keywords.
SELECT a.title,
a.release_date
FROM albums AS a
WHERE a.title = 'Charcoal Lane'
OR a.title = 'The New Danger';
mozilla
Based on the Mozilla SQL Style Guide. Keywords left-aligned at column 0, content indented 4 spaces underneath. One item per line. Uppercase keywords.
SELECT
a.title,
a.release_date
FROM albums AS a
WHERE
a.title = 'Charcoal Lane'
OR a.title = 'The New Danger';
aweber
Based on river style with JOINs as river keywords. INNER JOIN, LEFT JOIN, etc. participate in river alignment. Uppercase keywords.
SELECT r.last_name
FROM riders AS r
INNER JOIN bikes AS b
ON r.bike_vin_num = b.vin_num
AND b.engines > 2;
dbt
Based on dbt Labs' SQL style. Lowercase keywords, 4-space indent, blank lines between clauses, generous whitespace. Explicit join types.
select
a.title,
a.release_date
from albums as a
where
a.title = 'Charcoal Lane'
or a.title = 'The New Danger'
gitlab
Based on the GitLab SQL Style Guide. Uppercase keywords, 2-space indent, blank lines inside CTE bodies.
SELECT
a.title,
a.release_date
FROM albums AS a
WHERE
a.title = 'Charcoal Lane'
OR a.title = 'The New Danger';
kickstarter
Based on the Kickstarter SQL Style Guide. Uppercase keywords, 2-space indent, JOIN ON on same line, compact CTE chaining.
SELECT
a.title,
a.release_date
FROM albums AS a
INNER JOIN orders AS o ON a.id = o.album_id
WHERE
a.title = 'Charcoal Lane'
AND a.year > 2000;
mattmc3
Based on the Modern SQL Style Guide
by mattmc3. Lowercase river-style with leading commas. Uses plain join
instead of inner join.
select a.title
, a.release_date
from albums as a
join orders as o
on a.id = o.album_id
where a.title = 'Charcoal Lane'
and a.year > 2000;
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pgfmt-0.1.0.tar.gz.
File metadata
- Download URL: pgfmt-0.1.0.tar.gz
- Upload date:
- Size: 1.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
011f154a8612346da0985351deed2563bb3bbd16e62eb808b17058719a679d50
|
|
| MD5 |
6036c81105af19fcc85d7bda1f92fc52
|
|
| BLAKE2b-256 |
faf8d562771b3b68513c2c6a5b3a21da867a40a7a518dff770e08eb42c96c4d9
|
Provenance
The following attestation bundles were made for pgfmt-0.1.0.tar.gz:
Publisher:
deploy.yaml on gmr/pgfmt
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pgfmt-0.1.0.tar.gz -
Subject digest:
011f154a8612346da0985351deed2563bb3bbd16e62eb808b17058719a679d50 - Sigstore transparency entry: 1189023202
- Sigstore integration time:
-
Permalink:
gmr/pgfmt@73d34129a6f7c12569e35d1d9d82d92496114e67 -
Branch / Tag:
refs/tags/0.1.0a1 - Owner: https://github.com/gmr
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
deploy.yaml@73d34129a6f7c12569e35d1d9d82d92496114e67 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pgfmt-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pgfmt-0.1.0-py3-none-any.whl
- Upload date:
- Size: 33.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1da1f3ad113148c97f05d75220954d4f34f7436c979ddef78621182f00ed1347
|
|
| MD5 |
4b0da33a967e9fdd40842fc162fb5833
|
|
| BLAKE2b-256 |
1dd1abb7e717fbfdfaafddd8a88692b69ec3b2b6cd47798ca4e905a247618f41
|
Provenance
The following attestation bundles were made for pgfmt-0.1.0-py3-none-any.whl:
Publisher:
deploy.yaml on gmr/pgfmt
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pgfmt-0.1.0-py3-none-any.whl -
Subject digest:
1da1f3ad113148c97f05d75220954d4f34f7436c979ddef78621182f00ed1347 - Sigstore transparency entry: 1189023234
- Sigstore integration time:
-
Permalink:
gmr/pgfmt@73d34129a6f7c12569e35d1d9d82d92496114e67 -
Branch / Tag:
refs/tags/0.1.0a1 - Owner: https://github.com/gmr
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
deploy.yaml@73d34129a6f7c12569e35d1d9d82d92496114e67 -
Trigger Event:
release
-
Statement type: