Format whitespace in a SQL query to make it more readable with support for multiple SQL dialects
—
Command-line interface for formatting SQL files and integrating with development workflows.
Install the CLI tool globally or use with npx:
# Global installation
npm install -g sql-formatter
# Or use with npx (no installation required)
npx sql-formatter --helpsql-formatter [-h] [-o OUTPUT] [--fix]
[-l LANGUAGE] [-c CONFIG] [--version] [FILE]# Show help message
sql-formatter -h
sql-formatter --help
# Show version number
sql-formatter --version# Format file and output to stdout
sql-formatter input.sql
# Format from stdin
echo "SELECT * FROM users" | sql-formatter
# Specify output file
sql-formatter input.sql -o formatted.sql
sql-formatter input.sql --output formatted.sql
# Update file in-place
sql-formatter input.sql --fix# Specify SQL dialect
sql-formatter -l mysql input.sql
sql-formatter --language postgresql input.sql
# Available languages:
# bigquery, db2, db2i, duckdb, hive, mariadb, mysql, n1ql, plsql,
# postgresql, redshift, singlestoredb, snowflake, spark, sql,
# sqlite, tidb, transactsql, trino, tsql# Specify config file
sql-formatter -c .sql-formatter.json input.sql
sql-formatter --config config.json input.sql
# Specify config as JSON string
sql-formatter -c '{"keywordCase":"upper","tabWidth":4}' input.sql# Format a single file
sql-formatter query.sql
# Format from stdin
echo "select * from users where active=1" | sql-formatter
# Format with specific dialect
sql-formatter --language mysql query.sql# Format and save to new file
sql-formatter input.sql --output formatted.sql
# Update file in-place
sql-formatter query.sql --fix
# Process multiple files (using shell)
for file in *.sql; do
sql-formatter "$file" --fix
done# Use custom configuration file
sql-formatter --config .sql-formatter.json query.sql
# Inline configuration
sql-formatter -c '{"keywordCase":"upper","indentStyle":"tabularLeft"}' query.sql
# Combined language and config
sql-formatter --language postgresql --config config.json query.sqlThe CLI automatically looks for .sql-formatter.json in the current directory and parent directories.
Example .sql-formatter.json:
{
"language": "mysql",
"keywordCase": "upper",
"identifierCase": "lower",
"tabWidth": 4,
"indentStyle": "standard",
"linesBetweenQueries": 2
}Configuration Search Order:
--config option--config option.sql-formatter.json in current directory.sql-formatter.json in parent directories (recursive search)Format SQL files before commit:
# .git/hooks/pre-commit
#!/bin/sh
for file in $(git diff --cached --name-only --diff-filter=ACM | grep '\.sql$'); do
sql-formatter "$file" --fix
git add "$file"
doneAdd to package.json:
{
"scripts": {
"format:sql": "sql-formatter --fix src/**/*.sql",
"check:sql": "sql-formatter --config .sql-formatter.json src/**/*.sql"
}
}GitHub Actions example:
- name: Format SQL files
run: |
npm install -g sql-formatter
sql-formatter --fix database/migrations/*.sql
- name: Check SQL formatting
run: |
sql-formatter --config .sql-formatter.json database/**/*.sql > /dev/null
if [ $? -ne 0 ]; then
echo "SQL files are not properly formatted"
exit 1
ficonst { execSync } = require('child_process');
module.exports = {
// ... webpack config
plugins: [
{
apply: (compiler) => {
compiler.hooks.beforeCompile.tap('FormatSQL', () => {
execSync('sql-formatter --fix src/**/*.sql');
});
}
}
]
};const { exec } = require('child_process');
const gulp = require('gulp');
gulp.task('format-sql', (cb) => {
exec('sql-formatter --fix src/**/*.sql', cb);
});The CLI provides helpful error messages for common issues:
# File not found
sql-formatter nonexistent.sql
# Error: could not open file nonexistent.sql
# No input provided
sql-formatter
# Error: no file specified and no data in stdin
# Invalid configuration
sql-formatter -c '{"invalid": json}' query.sql
# Error: unable to parse as JSON or treat as JSON file
# Conflicting options
sql-formatter --output out.sql --fix input.sql
# Error: Cannot use both --output and --fix options simultaneously
# Missing file for --fix
sql-formatter --fix
# Error: The --fix option cannot be used without a filename0 - Success1 - Error (invalid input, configuration, or file system error)For large files or batch processing:
# Process files in parallel (if using GNU parallel)
find . -name "*.sql" | parallel sql-formatter {} --fix
# Use specific language to avoid detection overhead
sql-formatter --language mysql large-file.sql
# Use configuration file instead of inline JSON for repeated use
sql-formatter --config .sql-formatter.json query.sqlInstall with Tessl CLI
npx tessl i tessl/npm-sql-formatter