<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/introducing-sqlfmt-an-sql-gofmt-style-formatter-fmhcxx1lj" -->

---
title: Introducing sqlfmt: an SQL gofmt-style formatter | daily.dev
description: sqlfmt is a new opinionated SQL formatter inspired by gofmt, implementing the 'river alignment' style from The Art of PostgreSQL. It right-pads all clause...
canonical: https://daily.dev/posts/introducing-sqlfmt-an-sql-gofmt-style-formatter-fmhcxx1lj
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Introducing sqlfmt: an SQL gofmt-style formatter | daily.dev
og:description: sqlfmt is a new opinionated SQL formatter inspired by gofmt, implementing the 'river alignment' style from The Art of PostgreSQL. It right-pads all clause...
og:url: https://daily.dev/posts/introducing-sqlfmt-an-sql-gofmt-style-formatter-fmhcxx1lj
og:image: https://api.daily.dev/og/posts/FmhCxx1LJ.png
og:image:alt: Introducing sqlfmt: an SQL gofmt-style formatter
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Introducing sqlfmt: an SQL gofmt-style formatter

**[Planet PostgreSQL](https://daily.dev/sources/planet-postgresql)** · 5 min read · 10 upvotes · 0 comments

## Summary

sqlfmt is a new opinionated SQL formatter inspired by gofmt, implementing the 'river alignment' style from The Art of PostgreSQL. It right-pads all clause keywords (select, from, where, group by, etc.) to the same column at each nesting level, creating a vertical river with expressions flowing to the right. The tool uses a tokenizer rather than an AST for robustness and comment preservation. It ships with a CLI mirroring gofmt's interface (-w, -l, -d flags), editor plugins for Emacs and Vim/Neovim, and a WebAssembly-powered web formatter (~130 KB compressed). Install via `go install github.com/dimitri/sqlfmt/cmd/sqlfmt@latest`. The style enforces lowercase keywords, trailing commas, and and/or at line starts. A correctness oracle using pganalyze/pg_query_go verifies formatting never changes query semantics.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://postgr.es/p/9s3>

## Questions this post answers

### What is sqlfmt and how does it format SQL queries?

sqlfmt is a gofmt-style SQL formatter written in Go that applies a single opinionated style with no configuration options. Its defining feature is river alignment, where clause keywords like select, from, where, group by, and having are right-padded to the same column so they form a vertical river down the page. It is installed via go install and mirrors gofmt's CLI flags (-w, -l, -d).

_Developers standardizing SQL style across a team can track new formatter releases like this on daily.dev._

### Why do SQL formatters like sqlfmt use a tokenizer instead of parsing an AST?

sqlfmt uses a token-stream approach rather than an AST because PostgreSQL's own parser discards comments, requiring a separate comment-recovery pass anyway if an AST were used, and because a tokenizer degrades gracefully on partial or invalid SQL snippets while a grammar-based parser fails hard. Other production formatters like pg_format, sqlfluff, and prettier-plugin-sql follow the same token-stream design.

_Anyone weighing formatter architecture choices can follow tooling design tradeoffs like these on daily.dev._

### How does sqlfmt verify that formatting SQL doesn't change its meaning?

sqlfmt uses pganalyze/pg_query_go, which wraps the real PostgreSQL C parser, as a correctness oracle during testing. It checks that fingerprint(input) equals fingerprint(format.Format(input)), confirming that reformatting never silently alters what the query means. The test suite also runs a round-trip corpus test against real hand-formatted queries from a published book.

_Teams adopting automated SQL formatting can watch for verification techniques like this on daily.dev._

## Similar posts on daily.dev

- [pgfmt 2.1: in-place formatting and pg\_dump-compatible output](https://daily.dev/posts/pgfmt-2-1-in-place-formatting-and-pg-dump-compatible-output-iujcxxifh) · PostgreSQL · 28 upvotes · 0 comments
- [pgFormatter v5.10 has been released](https://daily.dev/posts/pgformatter-v5-10-has-been-released-qtlapjvbq) · PostgreSQL · 94 upvotes · 2 comments
- [pglinter](https://daily.dev/posts/pglinter-8smded7ey) · Hacker News · 12 upvotes · 0 comments
- [How to Format Your Queries in SSMS](https://daily.dev/posts/how-to-format-your-queries-in-ssms-h7ir7pxcg) · Brent Ozar · 0 upvotes · 0 comments

---

Tags: [#golang](https://daily.dev/tags/golang), [#sql](https://daily.dev/tags/sql), [#postgresql](https://daily.dev/tags/postgresql)

[View this post on daily.dev](https://daily.dev/posts/introducing-sqlfmt-an-sql-gofmt-style-formatter-fmhcxx1lj)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Introducing sqlfmt: an SQL gofmt-style formatter","url":"https://daily.dev/posts/introducing-sqlfmt-an-sql-gofmt-style-formatter-fmhcxx1lj","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/introducing-sqlfmt-an-sql-gofmt-style-formatter-fmhcxx1lj"},"datePublished":"2026-08-11T14:01:33.107Z","dateModified":"2026-09-14T09:08:32.682Z","description":"sqlfmt is a new opinionated SQL formatter inspired by gofmt, implementing the 'river alignment' style from The Art of PostgreSQL. It right-pads all clause...","image":"https://media.daily.dev/image/upload/s--OHB84bZF--/f_auto/v1722860399/public/Placeholder%2010","thumbnailUrl":"https://media.daily.dev/image/upload/s--OHB84bZF--/f_auto/v1722860399/public/Placeholder%2010","isAccessibleForFree":true,"articleSection":"Planet PostgreSQL","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Planet PostgreSQL","logo":"https://media.daily.dev/image/upload/logos/placeholder.jpg","url":"https://daily.dev/sources/planet-postgresql"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/introducing-sqlfmt-an-sql-gofmt-style-formatter-fmhcxx1lj","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":10},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"golang,sql,postgresql","timeRequired":"PT5M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Planet PostgreSQL","item":"https://daily.dev/sources/planet-postgresql"},{"@type":"ListItem","position":3,"name":"Introducing sqlfmt: an SQL gofmt-style formatter"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/introducing-sqlfmt-an-sql-gofmt-style-formatter-fmhcxx1lj#faq","mainEntity":[{"@type":"Question","name":"What is sqlfmt and how does it format SQL queries?","acceptedAnswer":{"@type":"Answer","text":"sqlfmt is a gofmt-style SQL formatter written in Go that applies a single opinionated style with no configuration options. Its defining feature is river alignment, where clause keywords like select, from, where, group by, and having are right-padded to the same column so they form a vertical river down the page. It is installed via go install and mirrors gofmt's CLI flags (-w, -l, -d). Developers standardizing SQL style across a team can track new formatter releases like this on daily.dev."}},{"@type":"Question","name":"Why do SQL formatters like sqlfmt use a tokenizer instead of parsing an AST?","acceptedAnswer":{"@type":"Answer","text":"sqlfmt uses a token-stream approach rather than an AST because PostgreSQL's own parser discards comments, requiring a separate comment-recovery pass anyway if an AST were used, and because a tokenizer degrades gracefully on partial or invalid SQL snippets while a grammar-based parser fails hard. Other production formatters like pg_format, sqlfluff, and prettier-plugin-sql follow the same token-stream design. Anyone weighing formatter architecture choices can follow tooling design tradeoffs like these on daily.dev."}},{"@type":"Question","name":"How does sqlfmt verify that formatting SQL doesn't change its meaning?","acceptedAnswer":{"@type":"Answer","text":"sqlfmt uses pganalyze/pg_query_go, which wraps the real PostgreSQL C parser, as a correctness oracle during testing. It checks that fingerprint(input) equals fingerprint(format.Format(input)), confirming that reformatting never silently alters what the query means. The test suite also runs a round-trip corpus test against real hand-formatted queries from a published book. Teams adopting automated SQL formatting can watch for verification techniques like this on daily.dev."}}]}
```

