A fast SQL formatter and minifier. Single binary, no runtime dependencies.
Requires a Rust toolchain.
cargo install rf-sqlfmtThe binary is installed to ~/.cargo/bin/sqlfmt.
git clone https://github.com/hwhang0917/sqlfmt.git
cd sqlfmt
cargo install --path .# Beautify SQL from stdin
cat query.sql | sqlfmt
# Beautify SQL from string argument
sqlfmt 'SELECT id, name FROM users WHERE id = 1;'
# Minify SQL
cat query.sql | sqlfmt -m
sqlfmt -m 'SELECT id, name FROM users WHERE id = 1;'sqlfmt [OPTIONS] [SQL]
Arguments:
[SQL] SQL string to format (reads from stdin if omitted)
Options:
-m, --minify Minify SQL instead of beautifying
--color <WHEN> When to use ANSI color output [auto|always|never] (default: auto)
-h, --help Print help
-V, --version Print version
Output is syntax-highlighted when stdout is a terminal and suppressed when
piped or redirected. NO_COLOR is honored.
Note: SQL line comments start with
--, which collides with flag parsing. To pass a SQL string that begins with--, end option parsing with a literal--first, or pipe via stdin:sqlfmt -- '-- a comment; SELECT * FROM users;' echo '-- a comment; SELECT * FROM users;' | sqlfmt
-- Input
SELECT id, name, email FROM users WHERE active = 1 AND role = 'admin' ORDER BY name;
-- Output
SELECT
id,
name,
email
FROM
users
WHERE
active = 1
AND role = 'admin'
ORDER BY
name;Column definitions are broken onto separate lines. Backtick (MySQL/MariaDB/ SQLite), double-quote (ANSI/PostgreSQL), and bracket (MSSQL) quoted identifiers are recognized.
-- Input
CREATE TABLE `user` (`id` INTEGER PRIMARY KEY NOT NULL, `name` TEXT NOT NULL, `age` INTEGER DEFAULT -1 NOT NULL);
-- Output
CREATE TABLE `user` (
`id` INTEGER PRIMARY KEY NOT NULL,
`name` TEXT NOT NULL,
`age` INTEGER DEFAULT -1 NOT NULL
);-- Input
SELECT
id,
name
FROM
users
WHERE
id = 1;
-- Output
SELECT id,name FROM users WHERE id=1;