sqlfmt
github.com
What it is
sqlfmt is a free formatter for PostgreSQL SQL, available as a command-line tool and as a browser formatter running through WebAssembly. Its river-aligned style pads clause keywords so they share a column. The CLI reads SQL from files or stdin and writes formatted SQL to stdout; it can also rewrite files in place, list files needing changes, and show unified diffs. Its explain subcommand formats PostgreSQL plan text and can provide advice, canonicalized output, or structural comparisons. Plan advice describes join order, join and access methods, and parallelism, without costs, row estimates, or timing. The explain diff operation returns exit code 0 for structurally identical plans and 1 when structures differ, so it can be used in scripts and CI. A Go subpackage provides formatting for Go programs, and the project includes Emacs and Vim integrations. The browser build exposes formatting and version functions to JavaScript. Deeply nested subqueries and less common upsert or DDL forms remain best-effort areas. sqlfmt is intended for standalone formatting and SQL used in The Art of PostgreSQL book and courses.
Spec sheet
- Purpose
- sqlfmt is a gofmt-style command-line formatter for PostgreSQL SQL.Read 2 Oct 2026 · github.com
- License
- sqlfmt is distributed under the PostgreSQL Licence.Read 2 Oct 2026 · github.com
- Browser use
- The PostgreSQL SQL Formatter runs sqlfmt in a browser through WebAssembly and is free to use without installation.Read 30 Sep 2026 · github.com
- Build requirements
- Building WebAssembly requires tinygo and wasm-opt on PATH.Read 2 Oct 2026 · github.com
- Canonicalization
- The -canonical option normalizes ordering and schema-qualification differences when comparing advice from different sources.Read 30 Sep 2026 · github.com
- CI use
- explain diff exits 0 when plans are structurally identical and 1 when they are not, so it composes into scripts and CI.Read 2 Oct 2026 · github.com
- 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.Read 30 Sep 2026 · github.com
- CLI modes
- The CLI formats files, rewrites files in place, lists files needing changes, shows unified diffs, reads stdin, and prints its version.Read 2 Oct 2026 · github.com
- Compatibility limits
- Deeply nested subqueries and exotic upsert or DDL forms remain best-effort formatting areas.Read 30 Sep 2026 · github.com
- Editor integrations
- The repository provides an Emacs sqlfmt-mode and a Vim ftplugin using formatprg and equalprg.Read 2 Oct 2026 · github.com
- EXPLAIN support
- The explain subcommand formats EXPLAIN output and provides advice, diff, and canonical operations.Read 2 Oct 2026 · github.com
- EXPLAIN tools
- The explain subcommand formats PostgreSQL plan text and can produce plan advice or structural diffs.Read 30 Sep 2026 · github.com
- Formatting style
- It implements a river-aligned style in which clause keywords are right-padded to end at the same column.Read 2 Oct 2026 · github.com
- Go library
- The format subpackage exposes format.Format(io.Reader), but its module path is described as not yet published.Read 2 Oct 2026 · github.com
- Known limits
- Deeply nested subqueries and exotic upsert or DDL forms remain best-effort.Read 2 Oct 2026 · github.com
- Maker background
- Dimitri Fontaine describes himself as a PostgreSQL Major Contributor and the author of The Art of PostgreSQL.Read 30 Sep 2026 · theartofpostgresql.com
- Maker profile
- Dimitri Fontaine identifies as a PostgreSQL Major Contributor and links to support at oss.theartofpostgresql.com.Read 2 Oct 2026 · github.com
- Plan advice
- The advice output describes join order, join method, access method, and parallelism without costs, row estimates, or timing.Read 2 Oct 2026 · github.com
- Plan comparison
- Explain diff exits 0 for structurally identical plans and 1 when their structures differ.Read 30 Sep 2026 · github.com
- Source corpus
- The formatting style was derived from 343 SQL files in The Art of PostgreSQL query corpus.Read 30 Sep 2026 · github.com
- Target use
- The tool is intended for SQL embedded in The Art of PostgreSQL book and courses and for standalone SQL formatting.Read 2 Oct 2026 · github.com
- WASM size
- The TinyGo and Binaryen build is around 330KB compared with around 2.9MB for the standard Go WebAssembly target.Read 2 Oct 2026 · github.com
- WebAssembly API
- The WebAssembly build exposes sqlfmt.format(sql) and sqlfmt.version for browser use.Read 2 Oct 2026 · github.com
- WebAssembly size
- The TinyGo and wasm-opt build is about 330 KB, compared with about 2.9 MB for the standard Go WebAssembly target.Read 30 Sep 2026 · github.com
Plans and prices
Strong points and limits
Strong points
- Formats PostgreSQL SQL in CLI and browser.
- Reads files or stdin; supports in-place rewriting.
- Explain diff can be used in CI.
- Includes Emacs and Vim integrations.
- Free under the PostgreSQL Licence.
Limits
- Supports PostgreSQL, not other listed dialects.
- Nested subqueries remain best-effort.
- Some upsert and DDL forms remain best-effort.
- Building WebAssembly requires tinygo and wasm-opt.
Who it suits
sqlfmt suits PostgreSQL users who want consistent formatting from a CLI, browser, or supported editor integration. Its explain tools may also suit people comparing query-plan structures in scripts or CI.
sqlfmt in depth
Read the full write-up
sqlfmt is a PostgreSQL formatter with command-line, browser, and editor workflows, plus tools for comparing and inspecting EXPLAIN plans. It is best suited to PostgreSQL users who want a consistent style and scriptable checks; its narrow dialect scope and best-effort handling of some syntax make it a poor fit for mixed-dialect or unusually complex SQL.
Overview
sqlfmt uses a river-aligned style: clause keywords are padded to a shared column. That gives SQL a consistent visual structure, but teams must be willing to adopt that particular style rather than choose among presets. The style draws on 343 SQL files from The Art of PostgreSQL query corpus, and the tool is intended for that book and its courses as well as standalone formatting.
The CLI reads files or standard input and writes formatted SQL to standard output. It can also rewrite files in place, list files that need changes, and produce unified diffs. Those modes suit both individual cleanup and review or CI workflows. PostgreSQL is the only supported dialect, and deeply nested subqueries and unusual upsert or DDL forms are best-effort cases; review those rewrites rather than applying them blindly.
Key features
Formatting across workflows
The browser formatter runs sqlfmt through WebAssembly without requiring installation. The repository also provides an Emacs mode and Vim integration through formatprg and equalprg, and the Go format subpackage exposes Format(io.Reader) (string, error) for Go programs. That range makes sqlfmt practical for users who want one formatter in an editor, a shell, or application code.
The WebAssembly build exposes sqlfmt.format(sql) and sqlfmt.version to browser JavaScript. A TinyGo and wasm-opt build is about 330 KB, compared with about 2.9 MB for the standard Go WebAssembly target, a useful reduction for browser delivery. Building WebAssembly requires tinygo and wasm-opt on PATH, so that compact output is less relevant to users who only need the hosted formatter or CLI.
PostgreSQL plan tools
The explain subcommand formats PostgreSQL EXPLAIN output and can provide advice, structural diffs, and canonical operations. Advice describes join order and method, access method, and parallelism, but not costs, row estimates, or timing; it helps explain plan shape, not diagnose performance by itself.
explain diff exits 0 for structurally identical plans and 1 when their structures differ, making it useful in scripts and CI. The -canonical option normalizes ordering and schema-qualification differences when comparing advice from different sources. These are focused inspection tools, not a substitute for runtime metrics.
Pricing
sqlfmt is free. The Browser formatter plan costs nothing and runs in the browser via WebAssembly with no installation required. The CLI, editor integrations, and Go library are also part of the project, so there is no paid tier to unlock those workflows.
Platforms
sqlfmt supports API, Linux, self-hosted, and web use. The browser formatter needs no local installation; the CLI and editor integrations suit Linux and self-hosted workflows. The project also supplies a WebAssembly API for browser JavaScript. PostgreSQL is the sole supported SQL dialect.
Who it's for
sqlfmt is a good choice for PostgreSQL developers who want a single formatter for shell scripts, editor use, browser formatting, or Go applications, and for teams that want structural plan comparisons in CI. It is less suitable where SQL spans multiple dialects or relies heavily on deeply nested queries and unusual upsert or DDL syntax. Its PostgreSQL-specific plan advice will also be of limited use to teams seeking cost, row-estimate, or timing analysis from that output.
Pros and cons
Pros: Free across browser and CLI workflows, with no paid tier gating the core tools.
Pros: File, stdin, in-place, and diff modes support both manual formatting and automated checks.
Pros: EXPLAIN formatting and structural comparison add a CI-friendly way to detect plan-shape changes.
Pros: Emacs, Vim, Go, and browser JavaScript interfaces extend use beyond the command line.
Cons: PostgreSQL-only support rules it out for projects that need a shared formatter across dialects.
Cons: Deeply nested subqueries and unusual upsert or DDL forms receive best-effort formatting, calling for review.
Cons: Plan advice omits costs, row estimates, and timing, limiting its value for performance diagnosis.
Alternatives
For other free SQL tools, browse SQL Linters and Formatters. Consider SQLCheck as another free command-line option, or Squawk if its broader platform support matters. Poor SQL is a free alternative with online and offline formatting and an SSMS/Visual Studio plugin; its open-source library is AGPL-licensed. Sqruff is another free option with web, self-hosted, and editor-extension platforms. TSQLLint is free and installable with dotnet-tool, Homebrew, or NPM. JSQLFormatter is another free choice for Linux, macOS, Windows, and self-hosted use. Sleek is a free open-source SQL formatter with editor-extension support. Choose SQL Formatter for its free formatting library and browser demo; it is MIT licensed.
Verdict
Choose sqlfmt if your SQL is PostgreSQL and you want a free, consistent formatter that works in scripts, editors, browsers, and CI, with plan-shape comparisons as a useful extra. Look elsewhere for multi-dialect coverage, or if complex nested queries and unusual DDL or upsert syntax need more than best-effort formatting.
Verdict
sqlfmt offers formatting across command-line, browser, and editor workflows, with additional PostgreSQL plan tools. Its best-effort cases matter if your SQL relies on deeply nested queries or unusual upsert and DDL forms.
Where Specifiction places it
- SQL Linters and Formatters#3 of 268.5