pg_format

repository·master·Indexed 23 days ago

https://github.com/darold/pgformatter

A PostgreSQL SQL and PL/pgSQL syntax beautifier supporting SQL-92 through SQL-2011 and PostgreSQL-specific keywords. It is available as a CLI tool, a CGI script for web environments, or as an integration for text editors like Vim. Features include in-place formatting, case control for keywords and functions, comment removal, and support for Redshift keywords.

Tokens
4.3K
Snippets
6
Records
12
Agent score
34%

What's inside pgformatter

  1. Configure pg_format via RC files

    master

    Instead of passing many command-line arguments, you can use a configuration file. pg_format automatically looks for configuration files in the following order:

    1. ./.pg_format (current directory)
    2. $HOME/.pg_format
    3. $XDG_CONFIG_HOME/pg_format/pg_format.conf

    You can override this behavior using the -c | --config FILE option or disable automatic RC file reading with -X | --no-rcfile.

  2. Configure pg_format using RC files

    master

    By default, pg_format looks for configuration files to apply persistent formatting rules. If the --no-rcfile or -X flag is used, this behavior is disabled.

    pg_format searches for configuration files in the following order:

    1. ./.pg_format (current directory)
    2. $HOME/.pg_format (user home directory)
    3. $USERPROFILE/.pg_format (Windows user profile)
    4. $XDG_CONFIG_HOME/pg_format/pg_format.conf (XDG config directory)

    Configuration File Format: The configuration file uses a simple key = value format. Lines starting with a letter are treated as settings. Note that command-line arguments always override settings found in these files.

    Example config file content:

    spaces = 2
    keyword-case = 1
    format = html
  3. Install pg_format from source

    master

    To install pg_format on a Linux system, download the tarball from GitHub, unpack it, and use the Perl Makefile.PL to build and install. By default, it installs to /usr/local/bin. To install under /usr/ (similar to Debian), use INSTALLDIRS='perl' during the Makefile.PL step or INSTALLDIRS=vendor.

    version=5.3 #please use the latest release version from github
    wget https://github.com/darold/pgFormatter/archive/refs/tags/v${version}.tar.gz
    tar xzf v${version}.tar.gz
    cd pgFormatter-${version}/
    perl Makefile.PL
    make && sudo make install
    
    # To install like Debian (under /usr/):
    perl Makefile.PL INSTALLDIRS=vendor
  4. Install pg_format via Docker

    master

    You can build a pgFormatter Docker image using the provided Dockerfile in the repository.

    docker build -t darold.net/pgformatter .
    
    # Use it by piping SQL to stdin:
    cat file.sql | docker run --rm -a stdin -a stdout -i darold.net/pgformatter -
  5. Integrate pg_format with Vim

    master

    You can use pg_format as the formatting engine for SQL files in Vim by adding the following to your ~/.vimrc:

    au FileType sql setl formatprg=/usr/local/bin/pg_format\ -

    Once configured, you can use standard Vim commands like gq to format text (e.g., ESC+gq+G to format the whole file).

    au FileType sql setl formatprg=/usr/local/bin/pg_format\ -
  6. Use pg_format via CLI

    master

    The pg_format command can format SQL files or read from stdin.

    Basic Usage: pg_format [options] file.sql

    Common Patterns:

    • Format a file and output to stdout: pg_format file.sql
    • Format a file and save to a specific file: pg_format -o result.sql file.sql
    • Format via pipe: cat file.sql | pg_format -
    • Format in-place (overwrites the input file): pg_format -i file.sql
    cat samples/ex1.sql | pg_format -
    pg_format -n samples/ex1.sql
    pg_format -f 2 -n -o result.sql samples/ex1.sql
  7. Use pg_format to beautify SQL queries

    master

    The pg_format utility is a PostgreSQL SQL and PL/pgSQL code beautifier. It can read SQL from files or from stdin and output the formatted result to stdout or a specified file.

    Basic Usage Patterns:

    • From stdin:
      cat samples/ex1.sql | pg_format -
    • From a file (output to stdout):
      pg_format samples/ex1.sql
    • From a file (output to a specific file):
      pg_format -o result.sql samples/ex1.sql
    • In-place formatting (overwrites the input file):
      pg_format -i samples/ex1.sql
    • Removing comments and formatting:
      pg_format -n samples/ex1.sql
  8. Prevent code snippets from being formatted

    master

    Use the -p or --placeholder option with a Perl regular expression to protect specific parts of your SQL from being modified. This is useful for protecting bit-shift operators, URLs in comments, or custom placeholders.

    Example: Protecting bit-shift operators pg_format samples/ex9.sql -p '<<(?:.*)?>>'

    Example: Protecting URLs in comments while wrapping at 60 chars pg_format samples/ex62.sql -C -w 60 -p 'https?://\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+~#?&//=]*)'

  9. Use pg_format as a pre-commit hook

    master

    You can use pg_format in a pre-commit configuration to ensure SQL files are formatted before committing. Use the --inplace flag to modify the files directly.

    - repo: https://github.com/darold/pgFormatter
        rev: "v5.8"
        hooks:
          - id: pg_format
            args: ["--inplace", "--wrap-limit", "120"]
            files: '.*\.sql$'
  10. Use pg_format as a CGI API

    master

    When running in a CGI environment, pg_format automatically detects the context and outputs HTML for an online formatter.

    To use it as a programmatic API, set the Accept HTTP header to application/json. This will return a JSON-formatted response.

    Requirements:

    • Install Perl CGI and JSON modules (libcgi-pm-perl and libjson-perl on Debian/Ubuntu, or perl-cgi and perl-json on Yum/CentOS).
    • Place pg_format.cgi in your cgi-bin folder.
    • Copy logo_pgformatter.png and icon_pgformatter.ico into the same CGI directory.
  11. Reference: CLI Options

    master

    List of available command-line options for pg_format.

    -a | --anonymize      : obscure all literals in queries, useful to hide
                                    confidential data before formatting.
    -b | --comma-start    : in a parameters list, start with the comma (see -e)
    -B | --comma-break    : in insert statement, add a newline after each comma.
    -c | --config FILE    : use a configuration file. Default is to not use
                                    configuration file unless files ./.pg_format or
                                    $HOME/.pg_format or the XDG Base Directory file
                                    $XDG_CONFIG_HOME/pg_format/pg_format.conf exist.
    -C | --wrap-comment   : with --wrap-limit, apply reformatting to comments.
    -d | --debug          : enable debug mode. Disabled by default.
    -e | --comma-end      : in a parameters list, end with the comma (default)
    -f | --function-case N: Change the case of the PostgreSQL functions. Default
                                    is unchanged: 0. Values: 0=>unchanged, 1=>lowercase,
                                    2=>uppercase, 3=>capitalize.
    -F | --format STR     : output format: text or html. Default: text.
    -g | --nogrouping     : add a newline between statements in transaction
                                    regroupement. Default is to group statements.
    -h | --help           : show this message and exit.
    -i | --inplace        : override input files with formatted content.
    -k | --keep-newline   : preserve empty line in plpgsql code.
    -L | --no-extra-line  : do not add an extra empty line at end of the output.
    -m | --maxlength SIZE : maximum length of a query, it will be cutted above
                                    the given size. Default: no truncate.
    -M | --multiline      : enable multi-line search for -p or --placeholder.
    -n | --nocomment      : remove any comment from SQL code.
    -N | --numbering      : statement numbering as a comment before each query.
    -o | --output file    : define the filename for the output. Default: stdout.
    -p | --placeholder RE : set regex to find code that must not be changed.
    -r | --redshift       : add RedShift keywords to the list of SQL keywords. 
                                    Obsolete now, use --extra-keyword 'redshift' instead.
    -s | --spaces size    : change space indent, default 4 spaces.
    -S | --separator STR  : dynamic code separator, default to single quote.
    -t | --format-type    : try another formatting type for some statements.
    -T | --tabs           : use tabs instead of space characters, when used 
                                    spaces is set to 1 whatever the value set to -s.
    -u | --keyword-case N : Change the case of the reserved keyword. Default is
                                    uppercase: 2. Values: 0=>unchanged, 1=>lowercase,
                                    2=>uppercase, 3=>capitalize.
    -U | --type-case N    : Change the case of the data type name. Default is
                                    lowercase: 1. Values: 0=>unchanged, 1=>lowercase,
                                    2=>uppercase, 3=>capitalize.
    -v | --version        : show pg_format version and exit.
    -w | --wrap-limit N   : wrap queries at a certain length.
    -W | --wrap-after N   : number of column after which lists must be wrapped. 
                                    Default: puts every item on its own line.
    -X | --no-rcfile      : don't read rc files automatically (./.pg_format or
                                    $HOME/.pg_format or $XDG_CONFIG_HOME/pg_format).
                                    The --config / -c option overrides it.
    --extra-function FILE : file containing a list of functions to use the same
                                    formatting as PostgreSQL internal function.
    --extra-keyword FILE  : file containing a list of keywords to use the same
                                    formatting as PostgreSQL internal keyword. Use
                                    special value 'redshift' for support to Redshift
                                    keywords defined internaly in pgFormatter.
    --no-space-function : remove space between function call and the open
                                    parenthesis.
    --redundant-parenthesis: do not remove redundant parenthesis in DML.
  12. Reference pg_format CLI options

    master

    The following options are available for the pg_format command line utility. Note that some options have specific value constraints (e.g., case options use integers 0-3).

    OptionLong FlagDescription
    -a--anonymizeObscure all literals in queries (useful for hiding confidential data).
    -b--comma-startIn a parameters list, start with the comma.
    -B--comma-breakIn an INSERT statement, add a newline after each comma.
    -c--config FILEUse a specific configuration file.
    -C--wrap-commentWith --wrap-limit, apply reformatting to comments.
    -d--debugEnable debug mode.
    -e--comma-endIn a parameters list, end with the comma (default).
    -f--function-case NChange case of PostgreSQL functions: 0=unchanged, 1=lowercase, 2=uppercase, 3=capitalize.
    -F--format STROutput format: text or html. Default: text.
    -g--nogroupingAdd a newline between statements in transaction regroupement.
    -h--helpShow help message and exit.
    -i--inplaceOverride input files with formatted content.
    -k--keep-newlinePreserve empty lines in PL/pgSQL code.
    -L--no-extra-lineDo not add an extra empty line at the end of output.
    -m--maxlength SIZEMaximum length of a query; queries exceeding this will be truncated.
    -M--multilineEnable multi-line search for --placeholder.
    -n--nocommentRemove any comments from SQL code.
    -N--numberingAdd statement numbering as a comment before each query.
    -o--output fileDefine the filename for the output. Default: stdout.
    -p--placeholder RESet a regex to find code that must not be changed.
    -r--redshiftAdd Redshift keywords (Obsolete: use --extra-keyword 'redshift').
    -s--spaces sizeChange space indent (default: 4).
    -S--separator STRDynamic code separator (default: single quote).
    -t--format-typeTry another formatting type for some statements.
    -T--tabsUse tabs instead of spaces (sets --spaces to 1).
    -u--keyword-case NChange case of reserved keywords: 0=unchanged, 1=lowercase, 2=uppercase, 3=capitalize.
    -U--type-case NChange case of data type names: 0=unchanged, 1=lowercase, 2=uppercase, 3=capitalize.
    -v--versionShow version and exit.
    -w--wrap-limit NWrap queries at a certain length.
    -W--wrap-after NNumber of columns after which lists must be wrapped.
    -X--no-rcfileDo not read automatic RC files.
    --extra-function FILEFile containing a list of functions to format like PostgreSQL internals.
    --extra-keyword FILEFile containing a list of keywords to format like PostgreSQL internals. Use 'redshift' for Redshift support.
    --no-space-functionRemove space between function call and the open parenthesis.
    --redundant-parenthesisDo not remove redundant parenthesis in DML.