PostgreSQL Formatter
Format PostgreSQL queries including CTEs, window functions and JSON operators.
What Is a PostgreSQL Formatter?
A PostgreSQL formatter rewrites PostgreSQL queries with consistent indentation, keyword casing, and clause alignment. It understands PostgreSQL-specific syntax including double-quote identifiers ("column"), the :: cast operator, CTEs (WITH clauses), window functions, JSONB operators (->, ->>, @>), and ON CONFLICT upsert syntax.
PostgreSQL Formatter Online — What This Tool Does
This free PostgreSQL formatter beautifies PG queries entirely in your browser using the sql-formatter library with the PostgreSQL dialect preset. Supports keyword casing, indent width, and comment stripping. Handles modern PG features including CTEs, window functions, and JSONB operators.
How to Format PostgreSQL Queries
- Paste your PostgreSQL query into the Input pane, or click Upload.
- Confirm the dialect is PG (selected by default for this route).
- Pick keyword case — ABC (uppercase) or abc (lowercase, popular in PostgreSQL community).
- Pick indent: 2 spaces, 4 spaces, or tab.
- Optional: enable No comments to strip
--and/* */comments. - Click Format. The formatted query appears instantly.
- Click Copy or Download.
PostgreSQL-Specific Syntax Handled
- Double-quote identifiers —
"User"for case-sensitive column or table names. ::cast operator —'42'::INTEGERfor explicit type casts.- CTEs —
WITH ... AS (...)common table expressions. - Window functions —
OVER (PARTITION BY ... ORDER BY ...). - JSONB operators —
->,->>,#>,@>,?. ON CONFLICT DO UPDATE— PostgreSQL upsert syntax.RETURNINGclause — return affected rows from INSERT/UPDATE/DELETE.- Array operators —
ANY(array),ARRAY[...],unnest().
PostgreSQL vs MySQL Formatting Differences
PostgreSQL uses double-quote identifiers; MySQL uses backticks. PostgreSQL has ON CONFLICT for upserts; MySQL has ON DUPLICATE KEY UPDATE. PostgreSQL has RETURNING, :: casts, CTEs (long supported in PG; recent in MySQL). Always select the matching dialect — using MySQL formatting on a PostgreSQL query can mangle quoted identifiers or unrecognised syntax.
Common Use Cases
- Reviewing complex CTEs — multi-step CTEs are unreadable without proper indentation.
- Window function queries —
OVER (PARTITION BY ... ORDER BY ...)clauses become readable when aligned. - JSONB queries — querying nested JSON fields with
->chains benefits from clean formatting. - Migrations — format Flyway, Alembic, or sqitch migrations before merging.
- Onboarding — make ORM-generated PG queries readable for new team members.
Tips & Tricks
- PostgreSQL convention is lowercase keywords — many PG codebases use
select,from,wherein lowercase. Pick abc for this style. - Watch quoted identifiers —
"User"is different fromuserin PostgreSQL. Don't strip the quotes if your column names need them. - Test with EXPLAIN — paste the formatted query into psql with
EXPLAINto verify it parses identically. - Combine with our JSON Formatter — useful when working with JSONB return values.
Related Tools
- SQL Formatter — generic ANSI SQL formatting.
- MySQL Formatter — MySQL/MariaDB-specific dialect.
- SQL Beautifier — alternate naming for the same operation.
- SQL Minifier — opposite operation, compress SQL.
- JSON to SQL — generate PG INSERTs from JSON.
Is My SQL Sent to a Server?
No. PostgreSQL formatting runs entirely in your browser using the sql-formatter library. Your queries — including production schema names, JSONB paths, and business logic — never leave your machine.