Home/Dev Tools/PostgreSQL Formatter

PostgreSQL Formatter

Format

Format PostgreSQL queries including CTEs, window functions and JSON operators.

Dialect
Keywords
Indent
Options
Input
Loading editor…
Output
Loading editor…
✨ AI Code Explanation
Mode

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'::INTEGER for explicit type casts.
  • CTEsWITH ... AS (...) common table expressions.
  • Window functionsOVER (PARTITION BY ... ORDER BY ...).
  • JSONB operators->, ->>, #>, @>, ?.
  • ON CONFLICT DO UPDATE — PostgreSQL upsert syntax.
  • RETURNING clause — return affected rows from INSERT/UPDATE/DELETE.
  • Array operatorsANY(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 queriesOVER (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, where in lowercase. Pick abc for this style.
  • Watch quoted identifiers"User" is different from user in PostgreSQL. Don't strip the quotes if your column names need them.
  • Test with EXPLAIN — paste the formatted query into psql with EXPLAIN to verify it parses identically.
  • Combine with our JSON Formatter — useful when working with JSONB return values.

Related Tools

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.