An opinionated SQL formatter written in Rust. Optimized for Snowflake and DuckDB.
git clone https://github.com/athvin/sqlfmt.git
cd sqlfmt
cargo install --path .Format files or directories in place:
sqlfmt .
sqlfmt queries/
sqlfmt path/to/query.sqlRead from stdin, write to stdout:
echo "SELECT a,b FROM t WHERE x=1" | sqlfmt -Check formatting without modifying files (exit code 1 if changes needed):
sqlfmt --check .Show a diff of what would change:
sqlfmt --diff .Usage: sqlfmt [OPTIONS] <FILES>...
Arguments:
<FILES>... Files or directories to format. Use "-" to read from stdin
Options:
-l, --line-length <LINE_LENGTH> Maximum line length [default: 88]
-d, --dialect <DIALECT> SQL dialect: polyglot, duckdb [default: polyglot]
--check Check formatting without writing changes
--diff Show formatting diff
--fast Skip safety equivalence check (faster)
--no-jinjafmt Disable Jinja template formatting
--exclude <EXCLUDE> Glob patterns to exclude
-v, --verbose Verbose output
-q, --quiet Quiet output (errors only)
-t, --threads <THREADS> Number of threads for parallel processing (0 = all cores) [default: 0]
--single-process Disable multi-threaded processing
--config <CONFIG> Path to config file (pyproject.toml or sqlfmt.toml)
-h, --help Print help
-V, --version Print version
You can set environment variables to configure behavior without passing flags on every invocation:
| Variable | Equivalent flag | Description |
|---|---|---|
SQLFMT_FAST=1 |
--fast |
Skip the safety equivalence check for faster formatting |
SQLFMT_THREADS=N |
--threads N |
Number of parallel threads (0 = all cores) |
SQLFMT_STRICT_WHITESPACE=1 |
(no flag) | Treat whitespace-only input as a parsing error instead of returning empty output |
Accepted values for boolean variables (SQLFMT_FAST, SQLFMT_STRICT_WHITESPACE): 1, true, yes (case-insensitive). CLI flags always take precedence over environment variables.
# Format a large directory as fast as possible
export SQLFMT_FAST=1
export SQLFMT_THREADS=8
sqlfmt .sqlfmt reads settings from sqlfmt.toml or the [tool.sqlfmt] section of pyproject.toml. The file is auto-discovered by searching parent directories of the files being formatted.
# sqlfmt.toml
line_length = 100
dialect = "duckdb"
exclude = ["migrations/**"]
no_jinjafmt = true| Option | Type | Description | Default |
|---|---|---|---|
line_length |
integer | Maximum line length | 88 |
dialect |
string | SQL dialect (polyglot or duckdb) |
polyglot |
exclude |
array of strings | Glob patterns to exclude | [] |
no_jinjafmt |
boolean | Disable Jinja template formatting | false |
Each SQL file is an independent unit of work. sqlfmt dispatches every file to a
worker that handles read → format → write end-to-end. By default it uses all
available cores; use --threads N to limit concurrency.
sqlfmt -t 8 . # Limit to 8 workers
sqlfmt --single-process . # Single-threadedFor maximum speed on large directories, combine with --fast to skip the safety
equivalence check:
sqlfmt --fast .