sqlfmt
github.com
- Free plan
- Linux
- Web
- API
- Self-hosted
What sqlfmt is
sqlfmt is a free formatter for PostgreSQL SQL, available as a command-line tool and a browser formatter that runs through WebAssembly without installation. Its river-aligned style places clause keywords 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. Its explain subcommand formats PostgreSQL plan text and can provide advice, canonicalized output, or structural comparisons. The advice describes join order, join and access methods, and parallelism without costs, row estimates, or timing. Explain diff returns different exit codes for matching and differing plan structures, making it usable in scripts and CI. A Go subpackage offers formatting for Go programs, and the project includes Emacs and Vim integrations. The WebAssembly build exposes formatting and version functions to browser JavaScript. Deeply nested subqueries and less common upsert or DDL forms are best-effort areas. sqlfmt is released under the PostgreSQL Licence.
Who it suits
sqlfmt suits PostgreSQL users who want consistent SQL formatting in a CLI, browser, Go program, or supported editor. Its plan comparison features may also suit people incorporating structural checks into scripts or CI.
Good to know, and what to watch
- Free browser formatter needs no installation.
- CLI can show unified diffs and rewrite files.
- Explain diff can compare plan structures.
- Includes Emacs and Vim integrations.
- Released under the PostgreSQL Licence.
- Supports PostgreSQL SQL only.
- Nested subqueries remain best-effort.
- Unusual upsert or DDL forms remain best-effort.
Plans
Every plan and priceBrowser formatter
Free plan
The Club's review
sqlfmt is a PostgreSQL formatter for developers who want a consistent style across command-line, editor, and browser workflows. Its plan-inspection commands also make it useful when comparing query structure, not just cleaning up SQL.
Overview
sqlfmt applies a river-aligned style, padding clause keywords to line them up in a column. That gives teams a consistent visual convention rather than a choice of formatting presets. The style draws on 343 SQL files from The Art of PostgreSQL query corpus, and the tool serves both SQL in that book and its courses and standalone formatting.
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 changes, show unified diffs, and print its version. Those modes suit both individual cleanup and review or automation workflows. Formatting is limited to PostgreSQL, and deeply nested subqueries or unusual upsert and DDL forms are best-effort cases; review those outputs before relying on bulk rewrites.
Key features
Formatting across workflows
The browser formatter runs sqlfmt through WebAssembly and requires no installation. The Go format subpackage exposes Format(io.Reader) (string, error) for applications that need formatting in Go 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; building it requires tinygo and wasm-opt on PATH.
Editor support includes an Emacs sqlfmt-mode and a Vim integration through formatprg and equalprg. That makes the formatter practical for people who want it in an existing editing routine, while the browser offers a quick option for occasional use.
EXPLAIN plan tools
The explain subcommand formats PostgreSQL plan text and can produce advice or structural diffs. Advice describes join order, join method, access method, and parallelism, but does not include costs, row estimates, or timing. It is therefore useful for examining plan structure, not a substitute for performance metrics.
Explain diff returns exit code 0 for structurally identical plans and 1 when their structures differ, making it suitable for scripts and CI checks. 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 via WebAssembly without installation. There are no paid tiers or plan quotas in the stated offer to weigh against its PostgreSQL-only scope.
Platforms
sqlfmt supports API, Linux, self-hosted, and web use. The CLI, Go package, browser formatter, WebAssembly API, and editor integrations cover a broad range of workflows, though the supported SQL dialect remains PostgreSQL only. The software is released under the PostgreSQL Licence.
Who it's for
sqlfmt is a strong fit for PostgreSQL developers who value a single formatting convention across local scripts, editors, browser tools, and CI. Teams comparing plan structures can also use its EXPLAIN advice and diff operations, provided they do not mistake structural advice for cost, estimate, or timing analysis. It is a weaker fit for teams working across multiple SQL dialects or relying on reliable formatting of deeply nested queries and unusual DDL or upsert syntax.
Pros and cons
- Pros: CLI modes cover stdout, in-place rewrites, changed-file lists, and unified diffs, supporting both interactive and automated use.
- Pros: Browser, Go, Emacs, Vim, and JavaScript interfaces offer practical ways to integrate PostgreSQL formatting into different environments.
- Pros: Structural plan diffs return distinct exit codes for matching and differing plans, which suits CI checks.
- Cons: PostgreSQL is the only supported dialect, so mixed-dialect teams need another formatter for their other SQL.
- Cons: Deep nesting and exotic upsert or DDL syntax are best-effort, making unchecked formatting risky for those queries.
- Cons: Plan advice omits costs, row estimates, and timing, limiting its value for performance diagnosis.
Alternatives
For a SQL linter and formatter directory, browse SQL Linters and Formatters. Choose Poor SQL instead if you need T-SQL formatting, including online or offline use and an SSMS or Visual Studio plugin. TSQLLint is another free option for users who want installable software through dotnet-tool, Homebrew, or NPM.
JSQLFormatter is a free alternative. Sqruff is another free choice if you want its extension, web, or self-hosted platform options. Sleek is a free alternative with an open-source SQL formatter plan. SQL Formatter suits readers seeking a free formatting library and browser demo under the MIT license. SQL Pretty Printer may suit readers who want a paid option with a one-time price of $50, paid once and a free trial. For another free formatter distributed under the PostgreSQL Licence, consider pgFormatter.
Verdict
Choose sqlfmt if PostgreSQL is your focus and you want one free formatter spanning CLI, editor, browser, and CI workflows, with structural EXPLAIN comparisons as a useful extra. Look elsewhere if you need broader dialect coverage or dependable handling of deeply nested and uncommon SQL forms.
The verdict
sqlfmt offers PostgreSQL formatting across command-line, browser, Go, and editor workflows, plus tools for examining and comparing plans. Formatting support is less complete for deeply nested queries and some uncommon SQL forms.
What the maker publishes
- Browser use
- The PostgreSQL SQL Formatter runs sqlfmt in a browser through WebAssembly and is free to use without installation. source
- Build requirements
- Building WebAssembly requires tinygo and wasm-opt on PATH. source
- Canonicalization
- The -canonical option normalizes ordering and schema-qualification differences when comparing advice from different sources. source
- CI use
- explain diff exits 0 when plans are structurally identical and 1 when they are not, so it composes into scripts and CI. source
- 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. source
- CLI modes
- The CLI formats files, rewrites files in place, lists files needing changes, shows unified diffs, reads stdin, and prints its version. source
- Compatibility limits
- Deeply nested subqueries and exotic upsert or DDL forms remain best-effort formatting areas. source
- Editor integrations
- The repository provides an Emacs sqlfmt-mode and Vim ftplugin integration through formatprg and equalprg. source
- Editor integrations
- The repository provides an Emacs sqlfmt-mode and a Vim ftplugin using formatprg and equalprg. source
- EXPLAIN support
- The explain subcommand formats EXPLAIN output and provides advice, diff, and canonical operations. source
- EXPLAIN tools
- The explain subcommand formats PostgreSQL plan text and can produce plan advice or structural diffs. source
- Formatting style
- It implements a river-aligned style that right-pads clause keywords to the same column. source
- Formatting style
- It implements a river-aligned style in which clause keywords are right-padded to end at the same column. source
- Go library
- The format subpackage exposes Format(io.Reader) (string, error) for formatting SQL in Go programs. source
- Go library
- The format subpackage exposes format.Format(io.Reader), but its module path is described as not yet published. source
- Known limits
- Deeply nested subqueries and exotic upsert or DDL forms remain best-effort. source
- License
- sqlfmt is released under the PostgreSQL Licence. source
- License
- sqlfmt is distributed under the PostgreSQL Licence. source
- Maker background
- Dimitri Fontaine describes himself as a PostgreSQL Major Contributor and the author of The Art of PostgreSQL. source
- Maker profile
- Dimitri Fontaine identifies as a PostgreSQL Major Contributor and links to support at oss.theartofpostgresql.com. source
- Plan advice
- The advice output describes join order, join method, access method, and parallelism without costs, row estimates, or timing. source
- Plan comparison
- Explain diff exits 0 for structurally identical plans and 1 when their structures differ. source
- Purpose
- sqlfmt is a gofmt-style command-line formatter for PostgreSQL SQL. source
- Purpose
- sqlfmt is a gofmt-style command-line formatter for PostgreSQL SQL. source
Checked against the maker's pages on 2 Oct 2026.