sqlfmt
What it is
sqlfmt is a free command-line formatter for PostgreSQL SQL, with a browser formatter that runs through WebAssembly without installation. Its river-aligned style pads clause keywords so they line up in a shared column. The CLI reads SQL from files or standard input and writes formatted output to standard output; it can also rewrite files in place, list files needing changes and show unified diffs. A Go subpackage provides formatting for Go programs, and the project includes Emacs and Vim integrations. The explain subcommand formats PostgreSQL plan text and can offer advice, canonicalized output or structural diffs. Advice describes join order, join method, access method and parallelism without costs, row estimates or timing. Explain diff returns distinct exit codes for structurally matching and differing plans, making it usable in scripts and CI. The WebAssembly build exposes formatting and version functions to browser JavaScript. Deeply nested subqueries and less common upsert or DDL forms remain best-effort areas. sqlfmt is released under the PostgreSQL Licence.
For and against
For
- Free browser formatter needs no installation.
- CLI supports in-place rewrites and unified diffs.
- Includes Emacs and Vim integrations.
- EXPLAIN diff can be used in CI.
- Released under the PostgreSQL Licence.
Against
- Supports PostgreSQL SQL only.
- Nested subqueries and uncommon upsert or DDL are best-effort.
- Building WebAssembly requires tinygo and wasm-opt.
The verdict
sqlfmt pairs SQL formatting with PostgreSQL plan formatting and structural comparison. Its compatibility limits matter for deeply nested queries and less common statement forms.
Who it suits
sqlfmt suits PostgreSQL developers who want consistent formatting in a CLI, browser, Go program or editor. Its EXPLAIN tools may also fit teams comparing plan structure in scripts or CI.
Plans and prices
Browser formatter
runs in browser via WebAssembly · no install required
Read 2 Oct 2026 · github.com
On the record
- Browser use
- The PostgreSQL SQL Formatter runs sqlfmt in a browser through WebAssembly and is free to use without installation.github.com · read 30 Sep 2026
- Build requirements
- Building WebAssembly requires tinygo and wasm-opt on PATH.github.com · read 2 Oct 2026
- Canonicalization
- The -canonical option normalizes ordering and schema-qualification differences when comparing advice from different sources.github.com · read 30 Sep 2026
- CI use
- explain diff exits 0 when plans are structurally identical and 1 when they are not, so it composes into scripts and CI.github.com · read 2 Oct 2026
- CLI input/output
- The command reads SQL from files or stdin and writes formatted SQL to stdout, with options for in-place rewriting, listing differences, and unified diffs.github.com · read 30 Sep 2026
- CLI modes
- The CLI formats files, rewrites files in place, lists files needing changes, shows unified diffs, reads stdin, and prints its version.github.com · read 2 Oct 2026
- Compatibility limits
- Deeply nested subqueries and exotic upsert or DDL forms remain best-effort formatting areas.github.com · read 30 Sep 2026
- Editor integrations
- The repository provides an Emacs sqlfmt-mode and a Vim ftplugin using formatprg and equalprg.github.com · read 2 Oct 2026
- EXPLAIN support
- The explain subcommand formats EXPLAIN output and provides advice, diff, and canonical operations.github.com · read 2 Oct 2026
- EXPLAIN tools
- The explain subcommand formats PostgreSQL plan text and can produce plan advice or structural diffs.github.com · read 30 Sep 2026
- Formatting style
- It implements a river-aligned style in which clause keywords are right-padded to end at the same column.github.com · read 2 Oct 2026
- Go library
- The format subpackage exposes format.Format(io.Reader), but its module path is described as not yet published.github.com · read 2 Oct 2026
- Known limits
- Deeply nested subqueries and exotic upsert or DDL forms remain best-effort.github.com · read 2 Oct 2026
- License
- sqlfmt is distributed under the PostgreSQL Licence.github.com · read 2 Oct 2026
- Maker background
- Dimitri Fontaine describes himself as a PostgreSQL Major Contributor and the author of The Art of PostgreSQL.theartofpostgresql.com · read 30 Sep 2026
- Maker profile
- Dimitri Fontaine identifies as a PostgreSQL Major Contributor and links to support at oss.theartofpostgresql.com.github.com · read 2 Oct 2026
- Plan advice
- The advice output describes join order, join method, access method, and parallelism without costs, row estimates, or timing.github.com · read 2 Oct 2026
- Plan comparison
- Explain diff exits 0 for structurally identical plans and 1 when their structures differ.github.com · read 30 Sep 2026
The review
sqlfmt is a PostgreSQL formatter with command-line, browser and editor workflows, plus tools for examining and comparing query plans. It suits PostgreSQL developers who want a consistent style and structural plan checks; its syntax edge cases call for caution before accepting automatic rewrites.
Overview
sqlfmt uses a river-aligned format, padding clause keywords so they begin in a shared column. This gives SQL a predictable visual structure, but there is no choice of formatting style. The approach was derived from 343 SQL files in The Art of PostgreSQL query corpus, and the tool is intended for both that book’s SQL and courses and standalone use.
The CLI reads files or standard input and writes formatted SQL to standard output. It can also rewrite files in place, identify files that need formatting, show unified diffs and print its version. Those options cover one-off cleanup as well as review and automation workflows. Support is limited to PostgreSQL, and deeply nested subqueries and unusual upsert or DDL forms are best-effort cases; review such output rather than applying it blindly.
Key features
Formatting across workflows
The free browser formatter runs sqlfmt through WebAssembly without requiring installation. The repository also provides an Emacs mode and Vim integration through formatprg and equalprg, making it possible to use the same formatting approach in common editing workflows. A Go subpackage exposes Format(io.Reader) (string, error) for Go programs, while the WebAssembly build provides sqlfmt.format(sql) and sqlfmt.version to browser JavaScript.
The TinyGo and wasm-opt build is about 330 KB, compared with about 2.9 MB for the standard Go WebAssembly target. That smaller build is useful when embedding the formatter in a browser-facing tool, though building it requires both tinygo and wasm-opt on PATH.
Plan formatting and comparison
The explain subcommand formats PostgreSQL EXPLAIN output and can produce advice, structural diffs and canonicalized comparisons. Advice describes join order, join method, access method and parallelism, but omits costs, row estimates and timing. It is therefore useful for discussing plan structure, not for assessing estimated or observed performance by itself.
explain diff exits 0 when two plans are structurally identical and 1 when their structures differ. That clear result makes it practical to use in scripts and CI when a structural change should be detected. The -canonical option normalizes ordering and schema-qualification differences when comparing advice from different sources, reducing noise in those comparisons.
Pricing
sqlfmt is free. The Browser formatter plan costs nothing and runs in the browser through WebAssembly with no installation required. There is no paid tier or seat or quota distinction in the stated plans, so the browser option does not trade away a capped allowance to reach the CLI or plan tools.
Platforms
sqlfmt is available for Linux, as a self-hosted tool, through an API and on the web. Its browser formatter needs no installation. The command-line and editor workflows favor developers working in a PostgreSQL environment, while the Go and WebAssembly interfaces provide ways to integrate formatting into software.
Who it's for
Choose sqlfmt if PostgreSQL is your dialect and you want one formatting convention across scripts, editors, browser use and automated checks. It is especially useful when structural comparison of EXPLAIN plans belongs in the same workflow. Look elsewhere if you need multiple SQL dialects or depend on reliably formatting deeply nested queries and uncommon upsert or DDL syntax without review.
Pros and cons
- Pros: Free browser use avoids installation, and the CLI’s rewrite, difference-listing and unified-diff modes suit both individual work and CI.
- Pros: Structural EXPLAIN comparison returns a script-friendly exit code, with canonicalization to reduce ordering and qualification differences.
- Pros: Emacs and Vim integrations, a Go formatting interface and a JavaScript WebAssembly API extend use beyond the standalone CLI.
- Cons: PostgreSQL is the only supported dialect, so teams working across SQL dialects need another formatter.
- Cons: Deeply nested subqueries and exotic upsert or DDL forms are best-effort, which limits confidence in unattended rewrites of those cases.
- Cons: Plan advice omits costs, row estimates and timing, so it cannot replace performance-focused plan analysis.
Alternatives
For another free SQL tool, consider Squawk. SQLCheck is a free command-line alternative. Choose pgFormatter for free software distributed under the PostgreSQL Licence. Poor SQL offers online and offline formatting, an SSMS/Visual Studio plugin and command-line use. Sleek is another free formatter. The other sqlfmt has a free plan with 20 analyses per day, five dialects, live formatting, saved snippets and history, making it a fit for readers seeking a multi-dialect service with those limits. SQLint is a free Linux option with a Standard SQL (BigQuery) parser and linter. Sqruff is another free alternative. Browse more options in SQL Linters and Formatters.
Verdict
sqlfmt is a strong fit for PostgreSQL users who want free, consistent formatting alongside scriptable checks for plan structure. Its focused dialect support and useful editor and browser paths are balanced by syntax cases that still need human review. Choose it for a PostgreSQL-centered workflow; look elsewhere if you need broad dialect coverage or dependable automatic handling of complex SQL.