Skip to main content

A PostgreSQL SQL formatter

Project description

pgfmt

A PostgreSQL SQL formatter with multiple style options.

Version Testing Coverage License

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pgfmt-0.1.0.tar.gz (1.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pgfmt-0.1.0-py3-none-any.whl (33.8 kB view details)

Uploaded Python 3

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

Hashes for pgfmt-0.1.0.tar.gz
Algorithm Hash digest
SHA256 011f154a8612346da0985351deed2563bb3bbd16e62eb808b17058719a679d50
MD5 6036c81105af19fcc85d7bda1f92fc52
BLAKE2b-256 faf8d562771b3b68513c2c6a5b3a21da867a40a7a518dff770e08eb42c96c4d9

See more details on using hashes here.

Provenance

The following attestation bundles were made for pgfmt-0.1.0.tar.gz:

Publisher: deploy.yaml on gmr/pgfmt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for pgfmt-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1da1f3ad113148c97f05d75220954d4f34f7436c979ddef78621182f00ed1347
MD5 4b0da33a967e9fdd40842fc162fb5833
BLAKE2b-256 1dd1abb7e717fbfdfaafddd8a88692b69ec3b2b6cd47798ca4e905a247618f41

See more details on using hashes here.

Provenance

The following attestation bundles were made for pgfmt-0.1.0-py3-none-any.whl:

Publisher: deploy.yaml on gmr/pgfmt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page