Skip to content

PgFormatter

A PostgreSQL SQL syntax beautifier that can work as a console program or as a CGI. On-line demo site at http://sqlformat.darold.net/

Usage

# 설치
pip install pgformatter
# 또는 Perl 원본
sudo apt install pgformatter

# 사용
pg_format input.sql -o output.sql

sql 파일 모두 적용 예제

#!/usr/bin/env bash

ROOT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" || exit; pwd)
POSTGRES_DIR="$ROOT_DIR/postgres"

if ! command -v pg_format &> /dev/null; then
    echo "Not found pg_format command" 1>&2
    echo "Install: sudo apt install pgformatter" 1>&2
    exit 1
fi

find "$POSTGRES_DIR" -name '*.sql' -print0 | while IFS= read -r -d '' file; do
    pg_format --inplace --spaces 4 --keyword-case 2 --type-case 1 --keep-newline "$file"
    echo "Formatted: ${file#"$ROOT_DIR/"}"
done

See also

Favorite site