pgfmt
A PostgreSQL SQL formatter. Reads SQL from stdin, outputs formatted SQL to stdout.
Uses libpg_query to parse SQL with the actual PostgreSQL parser.
Install
go install github.com/middle-management/pgfmt@latest
Usage
echo "SELECT id,name FROM users WHERE active=true" | pgfmt
pgfmt < query.sql
LSP Server
pgfmt includes an LSP server that provides formatting and diagnostics for SQL files.
Install
go install github.com/middle-management/pgfmt/cmd/pgfmt-lsp@latest
Editor Setup
Neovim (via nvim-lspconfig):
vim.lsp.config['pgfmt'] = {
cmd = { 'pgfmt-lsp' },
filetypes = { 'sql' },
root_markers = { '.git' },
}
vim.lsp.enable('pgfmt')
VS Code (via a generic LSP client like vscode-languageclient):
{
"pgfmt.server.path": "pgfmt-lsp"
}
Zed — install the pgfmt Zed extension from the extension registry. It bundles pgfmt-lsp and downloads it automatically — no manual setup required.
Then enable formatting in .zed/settings.json or ~/.config/zed/settings.json:
{
"languages": {
"SQL": {
"formatter": "language_server",
"format_on_save": "on"
}
}
}
See the extension README for advanced configuration, including how to use a custom binary path.
Features
- Formatting — format SQL files using
textDocument/formatting
- Diagnostics — parse errors reported on open and change
Build from source
git clone https://github.com/middle-management/pgfmt.git
cd pgfmt
go build -o pgfmt .
go build -o pgfmt-lsp ./cmd/pgfmt-lsp