Skip to main content
The cockroach sqlfmt changes the textual formatting of one or more SQL queries. It recognizes all SQL extensions supported by CockroachDB. A web interface to this feature is also available.
This feature is in and subject to change. To share feedback and/or issues, contact Support.

Synopsis

Use the query formatter interactively:
Reformat a SQL query given on the command line:
Reformat a SQL query already stored in a file:

Flags

The sqlfmt command supports the following flags.
FlagDescriptionDefault value
--execute -eReformat the given SQL query, without reading from standard input.N/A
--print-widthDesired column width of the output.80
--tab-widthNumber of spaces occupied by a tab character on the final display device.4
--use-spacesAlways use space characters for formatting; avoid tab characters.Use tabs.
--alignUse vertical alignment during formatting.Do not align vertically.
--no-simplifyAvoid removing optional grouping parentheses during formatting.Remove unnecessary grouping parentheses.

Examples

Reformat a query with constrained column width

Using the interactive query formatter, output with the default column width (80 columns):
  1. Start the interactive query formatter:
  2. Press Enter.
  3. Run the query:
  4. Press CTRL+D.
Using the command line, output with the column width set to 40:

Reformat a query with vertical alignment

Output with the default vertical alignment:
Output with vertical alignment:

Reformat a query with simplification of parentheses

Output with the default simplification of parentheses:
Output with no simplification of parentheses:

See also