PostgreSQL Formatter

This PostgreSQL formatter indents a query, sets the keyword case and puts every clause on its own line, inside your browser. Minify collapses it back onto one line.

Keywords
Indent
Result

Loading the formatter.

The result follows what you type. Ctrl+Enter, Cmd+Enter on a Mac, runs it at once. The query stays in this tab.

What it does and how to use it

The box above opens with a query carrying most of what people come here to check: a CTE, DISTINCT ON, a window function, a $1 placeholder, two jsonb operators and an ON CONFLICT ... DO UPDATE. Edit it in place or paste over it. Format (Ctrl+Enter, or Cmd+Enter on a Mac) rewrites the statement one clause per line, while the dropdowns underneath set keyword case and indent width, either of them re-running the formatter on what is already there. When the library cannot read your input, its message lands under the result and your text stays put.

How to format a PostgreSQL query without rewriting it

Indentation earns its keep the moment you read a query beside its plan, because EXPLAIN prints one node per line, and a statement laid out the same way lets you match the sequential scan you are worried about to the table it came from without counting parentheses.

Postgres syntax that trips generic formatters mostly survives, with one quirk worth knowing. In the sample above, DISTINCT ON (o.customer_id) keeps its text, though the line break falls after DISTINCT rather than after the closing parenthesis. Casts stay glued to what they cast, so created_at::date never returns as created_at :: date. Arriving from an ORM log, $1 and $2 are left as placeholders rather than read as broken syntax, and ->>, @> and #>> are spaced as the operators they are.

For all that, nothing here checks whether a statement is valid, so select from where comes back neatly indented. The syntax checker runs the real PostgreSQL grammar and reports where it stopped.

FAQ

Frequently asked questions

The dialect running here knows both. WITH, WITH RECURSIVE, OVER (PARTITION BY ... ORDER BY ...), ON CONFLICT ... DO UPDATE and RETURNING are indented as Postgres syntax rather than guessed at.

Text between dollar quotes is a string literal to the formatter, so a DO block comes back with its line breaks, indentation and comments intact. Only the SQL around it is reformatted.

No. The library loads into this page and your query is processed in the tab you are reading. Open the network panel, press Format, and watch nothing go out.

Minify drops both comment forms and collapses runs of whitespace to single spaces, which gets a long statement onto one line for a shell script. Quoted text is copied through byte for byte.

pgFormatter is a Perl program you install and run on your own machine. A JavaScript library, sql-formatter, does the work here, so the layout differs in places. Neither changes what a query does.

It costs nothing and there is nothing to sign up for. Nobody is counting how many queries you put through it, and none of them are kept.

Formatting proves nothing about whether a statement runs. The syntax checker reports the character position the parser stopped at, and the playground runs a Postgres server inside the tab when you want to execute the query.