PostgreSQL Formatter
Format and beautify your PostgreSQL queries instantly.
Formatting preserves identifiers and aliases. Style suggestions are not database validation. Maximum input: 100,000 characters.
Turning history off also clears saved queries. Shared links contain your SQL; only share queries you intend others to read.
What would make this tool more useful?
One click helps decide what gets built next.
Tell us more in your own words (optional)
What you write here is never sent to analytics and never reaches our servers. The button opens your own email app with the text filled in, so you can see and edit exactly what is sent before it leaves your device. Please do not include confidential SQL.
Compose emailClient-Side Only
SQL formatting happens locally in your browser. Analytics records usage metadata, not SQL text. See our privacy details.
9 Dialects
PostgreSQL, MySQL, SQL Server, BigQuery, SQLite, MariaDB, Redshift, and Snowflake.
Saves Settings
Your indent size, keyword case, and dialect preferences are saved locally between sessions.
Query History
Optionally keeps your last 5 queries in this browser. Turn history off or clear it at any time.
Free Online PostgreSQL Formatter
This PostgreSQL formatter instantly beautifies your Postgres SQL queries, making them easier to read, debug, and maintain. Simply paste your unformatted query and click Format.
PostgreSQL-Specific Formatting
Our formatter understands PostgreSQL-specific syntax including:
- Data Types: SERIAL, BIGSERIAL, UUID, JSONB, ARRAY, and other Postgres types
- Operators: JSONB operators (->>, ->), array operators, regex operators
- Window Functions: OVER, PARTITION BY, ROW_NUMBER, LAG, LEAD
- CTEs: Common Table Expressions with WITH clause
- UPSERT: ON CONFLICT DO UPDATE/NOTHING syntax
- Lateral Joins: LATERAL subqueries and joins
Why Format PostgreSQL Queries?
PostgreSQL queries can become complex with CTEs, window functions, JSONB operations, and advanced joins. Proper formatting makes these queries easier to understand and maintain. This is especially important when working with:
- Complex reporting queries with multiple CTEs
- Data transformation pipelines
- Performance optimization and query analysis
- Code reviews and documentation
Worked examples
JSON access with ->> and ->
PostgreSQL JSON operators are easy to lose in a dense line. The formatter keeps every arrow and quoted key exactly as written while giving each column its own line.
Before
select id,payload->>'email' as email,payload->'meta'->>'source' as source from events where payload->>'status'='active' and created_at > now() - interval '7 days' order by created_at desc
After
SELECT
id,
payload ->> 'email' AS email,
payload -> 'meta' ->> 'source' AS source
FROM
events
WHERE
payload ->> 'status' = 'active'
AND created_at > now() - interval '7 days'
ORDER BY
created_at DESC
DISTINCT ON, a PostgreSQL-only clause
DISTINCT ON takes a parenthesised column list that must match the leading ORDER BY columns. Seeing them stacked makes a mismatch obvious.
Before
select distinct on (user_id) user_id,session_id,started_at,device from sessions where started_at >= current_date - 30 order by user_id,started_at desc
After
SELECT DISTINCT
ON (user_id) user_id,
session_id,
started_at,
device
FROM
sessions
WHERE
started_at >= CURRENT_DATE - 30
ORDER BY
user_id,
started_at DESC
Dollar-quoted strings stay untouched
Text inside $$ ... $$ is never reformatted or re-indented, so escaped bodies and embedded punctuation survive exactly as you wrote them.
Before
select id,$$a > b -- not a comment$$ as note,coalesce(nullif(label,''),'unlabelled') as label from items where kind = any(array['a','b'])
After
SELECT
id,
$$a > b -- not a comment$$ AS note,
coalesce(nullif(label, ''), 'unlabelled') AS label
FROM
items
WHERE
kind = ANY (ARRAY['a', 'b'])
Other SQL Dialects
Need to format SQL for other databases? Try our formatters for MySQL, SQL Server, BigQuery, or SQLite.