PostgreSQL formatter
Format PostgreSQL in your browser. Casts, dollar-quoted bodies, JSONB operators and arrays are read the way Postgres reads them, so the formatter only ever changes whitespace and the casing you ask for.
Every layout setting of the formatter, grouped like Editor › Formatting in the SQLly app. The style above is the starting point; anything you change here is marked.
Drop a .sql file here to open it. It is read in your browser, not uploaded.
SELECT
id,
payload ->> 'kind' AS kind,
payload #>> '{user,id}' AS user_id,
tags[1] AS first_tag
FROM
events
WHERE
tags @> ARRAY['beta']::TEXT[]
AND payload ? 'source'
AND created_at > NOW() - INTERVAL '7 days'
ORDER BY
created_at DESC NULLS LAST
LIMIT 50;
Loading the formatter…
Shortcuts in the editor: Ctrl+Enter formats and Ctrl+Shift+Enter minifies (⌘ instead of Ctrl on a Mac). Your settings are remembered in this browser and kept in the page address, so a link shares your style – never your SQL.
What the formatter does with PostgreSQL SQL
- Operators are read the way Postgres reads them. PostgreSQL treats a run of operator characters as a single operator, and so does the formatter:
->>,#>>,@>,<@,?|,||and your own custom operators are never pulled apart, whilex>=-1still reads asx >= -1. - Casts and arrays stay put.
created_at::datestays tight, andARRAY['a','b'],tags[1],tags[2:3]andtext[]are kept exactly as written. - Dollar-quoted bodies are untouched. The body of a
$$ … $$or$tag$ … $tag$string, such as a function orDOblock, comes out exactly as it went in. - Parameters and literals survive.
$1placeholders, psql:variables,E'…'escape strings,B'…'andX'…'bit strings andU&'…'Unicode strings are each one token. - Case-safe quoting. PostgreSQL folds unquoted names to lowercase, so Remove unnecessary quotes only unquotes names that are already lowercase.
"Users"keeps its quotes. - Postgres keywords get your casing, including
RETURNING,ILIKE,LATERAL,INTERVAL '7 days'andNULLS LAST.
Good to know
- The SQL inside a function body is a string to the formatter, so it is kept as written rather than reformatted.
Examples
PostgreSQL before and after
select id, payload->>'kind' as kind, payload#>>'{user,id}' as user_id, tags[1] as first_tag from events where tags @> array['beta']::text[] and payload ? 'source' and created_at > now() - interval '7 days' order by created_at desc nulls last limit 50;
SELECT
id,
payload ->> 'kind' AS kind,
payload #>> '{user,id}' AS user_id,
tags[1] AS first_tag
FROM
events
WHERE
tags @> ARRAY['beta']::TEXT[]
AND payload ? 'source'
AND created_at > NOW() - INTERVAL '7 days'
ORDER BY
created_at DESC NULLS LAST
LIMIT 50;
The output uses the Standard style with 4-space indents, exactly as the formatter above produces it. Switch the style to Compact for denser SQL or Expanded for leading commas and one item per line. The formatter docs cover the details.
PostgreSQL formatter FAQ
Does the PostgreSQL formatter break JSONB operators like ->> or @>?
No. It reads operators the way PostgreSQL does, as one run of operator characters, so ->>, #>>, @>, <@, ?| and custom operators are never split. If its output would not lex to the same SQL, it gives your input back unchanged.
Does it reformat the body of a function between $$ quotes?
No. A dollar-quoted body is a string literal, and the formatter never changes the inside of a string. The CREATE FUNCTION statement around it is still laid out.
Will it remove the quotes from "MixedCase" names?
Only if you ask, and only when it is safe. PostgreSQL lowercases unquoted names, so Remove unnecessary quotes only unquotes names that are already lowercase and are not keywords.
Is my SQL sent to a server?
No. The formatter is compiled to WebAssembly and runs inside your browser tab. Your SQL is never uploaded, logged or stored, and once the page has loaded it keeps working offline.
Can formatting break my query?
It is built not to. After formatting, SQLly checks its output token by token against your input. If anything other than whitespace and the casing you asked for would change, it gives your SQL back exactly as you wrote it and tells you why. SQL that does not parse cleanly, such as an unterminated string, is also left untouched, with the line and column of the problem.
Can I share my formatting settings with my team?
Yes. Every setting, including the advanced ones, is kept in the page address after the # sign, so copying the address or pressing Copy settings link shares your exact style. The link holds the settings only, never your SQL.
Other dialects
- Any dialect (auto-detect)
- SQL Server formatter (T-SQL)
- MySQL formatter
- SQLite formatter
- Oracle SQL formatter
- Snowflake SQL formatter
- BigQuery SQL formatter
- Redshift SQL formatter
Working with PostgreSQL every day? See what the SQLly desktop app does for PostgreSQL, or browse all features.
Like the formatter? Meet the rest of SQLly
A native SQL workbench for macOS, Windows and Linux with IntelliSense that knows your schema, guardrails for risky queries, and this formatter built into the editor.