SQL Formatter/ MySQL · PostgreSQL

Beautify or minify SQL queries instantly. Supports MySQL, PostgreSQL and generic SQL.

Mode:
Dialect:
Indent:
Input SQL0 chars
Formatted

Did this tool solve your problem?

Code Examples

Python (sqlparse)

import sqlparse

sql = "select id,name from users where active=1"

# Format / beautify
formatted = sqlparse.format(
    sql,
    reindent=True,
    keyword_case='upper',
    identifier_case='lower',
    strip_comments=False,
    indent_width=2,
)
print(formatted)
# SELECT id, name
# FROM users
# WHERE active = 1

# Minify
minified = sqlparse.format(
    sql,
    strip_whitespace=True,
    keyword_case='upper',
)

Node.js (sql-formatter)

import { format } from 'sql-formatter';

const sql = `select id,name,email
from users where status='active'`;

// Format for MySQL
const formatted = format(sql, {
  language: 'mysql',
  tabWidth: 2,
  keywordCase: 'upper',
  linesBetweenQueries: 2,
});

// Format for PostgreSQL
const pgFormatted = format(sql, {
  language: 'postgresql',
  tabWidth: 2,
  keywordCase: 'upper',
});

console.log(formatted);
// SELECT
//   id,
//   name,
//   email
// FROM users
// WHERE status = 'active'

CLI (pgFormatter)

# Install pgFormatter (Perl)
# https://github.com/darold/pgFormatter
cpanm pgFormatter

# Format a file
pg_format query.sql -o formatted.sql

# Format from stdin
echo "select * from users" | pg_format

# Options
pg_format query.sql   --spaces 2   --keyword-case 1   --function-case 2

# Using prettier-plugin-sql (Node)
npx prettier --plugin prettier-plugin-sql   --parser sql query.sql

Go

package main

import (
    "fmt"
    "strings"
    "unicode"
)

// Simple SQL keyword uppercaser
func formatSQL(sql string) string {
    keywords := []string{
        "SELECT", "FROM", "WHERE",
        "JOIN", "LEFT JOIN", "INNER JOIN",
        "GROUP BY", "ORDER BY", "HAVING",
        "INSERT INTO", "VALUES", "UPDATE",
        "SET", "DELETE FROM", "LIMIT",
    }
    result := sql
    for _, kw := range keywords {
        lower := strings.ToLower(kw)
        result = strings.ReplaceAll(
            result, lower,
            "\n"+kw,
        )
    }
    return strings.TrimSpace(result)
}

// For production use, consider:
// github.com/xwb1989/sqlparser (MySQL)
// github.com/pganalyze/pg_query_go (PostgreSQL)
func main() {
    sql := "select id from users where id=1"
    fmt.Println(formatSQL(sql))
}

Frequently Asked Questions

What is SQL formatting / beautifying?

SQL formatting (beautifying) transforms a compact or inconsistently-written SQL query into a readable, consistently indented form. It adds line breaks before major clauses (SELECT, FROM, WHERE, JOIN etc.) and uppercases keywords. This doesn't change what the query does — only how it looks.

Does it support MySQL and PostgreSQL syntax?

The formatter handles common SQL syntax shared by MySQL, PostgreSQL, and most other RDBMS. Dialect-specific features like PostgreSQL's :: cast operator or MySQL's backtick identifiers are tokenized correctly. Select your dialect to signal which syntax conventions you're using.

What is SQL minification used for?

Minification removes all extra whitespace and comments to produce the most compact form of a query. This is useful when embedding SQL in code strings, sending SQL over a wire in logs, or reducing query log verbosity. Minified queries are functionally identical to their formatted versions.

How do I format SQL queries from the command line?

For PostgreSQL, use pg_format (Perl). For MySQL, use the mysqlformat Python package. You can also use sqlparse in Python: import sqlparse; sqlparse.format(sql, reindent=True, keyword_case='upper'). See code examples below.

Why are my comments removed after minification?

Minification intentionally strips SQL comments (-- single-line and /* block */ comments) to reduce size. If you need to preserve comments, use the format mode instead, which keeps all comments and reformats them at the correct indentation level.

Can the formatter handle CTEs (WITH clauses)?

Yes. WITH ... AS (...) common table expressions are recognized. The WITH keyword starts a new block. Nested SELECT statements inside CTEs are also indented appropriately.