{"id":15037174,"url":"https://github.com/darold/pgformatter","last_synced_at":"2025-05-14T01:02:35.552Z","repository":{"id":6064750,"uuid":"7290458","full_name":"darold/pgFormatter","owner":"darold","description":"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/","archived":false,"fork":false,"pushed_at":"2025-03-31T19:29:21.000Z","size":3505,"stargazers_count":1753,"open_issues_count":32,"forks_count":104,"subscribers_count":23,"default_branch":"master","last_synced_at":"2025-04-09T21:12:09.397Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PLpgSQL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"postgresql","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/darold.png","metadata":{"files":{"readme":"README","changelog":"ChangeLog","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2012-12-22T21:14:00.000Z","updated_at":"2025-04-07T05:17:16.000Z","dependencies_parsed_at":"2024-03-07T14:54:31.068Z","dependency_job_id":"f0f40dbc-c3a3-47e3-a23d-8d733764ae48","html_url":"https://github.com/darold/pgFormatter","commit_stats":{"total_commits":614,"total_committers":28,"mean_commits":"21.928571428571427","dds":0.2752442996742671,"last_synced_commit":"10fc46b5b39dcb1c4bd3196394c11880928ba99a"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darold%2FpgFormatter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darold%2FpgFormatter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darold%2FpgFormatter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darold%2FpgFormatter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/darold","download_url":"https://codeload.github.com/darold/pgFormatter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248111974,"owners_count":21049578,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-24T20:33:40.616Z","updated_at":"2025-04-09T21:12:17.582Z","avatar_url":"https://github.com/darold.png","language":"PLpgSQL","funding_links":[],"categories":[],"sub_categories":[],"readme":"NAME\n    pg_format - PostgreSQL SQL syntax beautifier\n\nDESCRIPTION\n    This SQL formatter/beautifier supports keywords from SQL-92, SQL-99,\n    SQL-2003, SQL-2008, SQL-2011 and PostgreSQL specifics keywords. Works\n    with any other databases too.\n\n    pgFormatter can work as a console program or as a CGI. It will\n    automatically detect its environment and format output as text or as\n    HTML following the context. It can also return a JSON-formatted response\n    if used as CGI with 'Accept: application/json'.\n\n    Keywords highlighting will only be available in CGI context.\n\n  Terminal/console execution\n    Usage: pg_format [options] file.sql\n\n        PostgreSQL SQL queries and PL/PGSQL code beautifier.\n\n    Arguments:\n\n        file.sql can be a file, multiple files or use - to read query from stdin.\n\n        Returning the SQL formatted to stdout or into a file specified with\n        the -o | --output option.\n\n    Options:\n\n        -a | --anonymize      : obscure all literals in queries, useful to hide\n                                confidential data before formatting.\n        -b | --comma-start    : in a parameters list, start with the comma (see -e)\n        -B | --comma-break    : in insert statement, add a newline after each comma.\n        -c | --config FILE    : use a configuration file. Default is to not use\n                                configuration file unless files ./.pg_format or\n                                $HOME/.pg_format or the XDG Base Directory file\n                                $XDG_CONFIG_HOME/pg_format/pg_format.conf exist.\n        -C | --wrap-comment   : with --wrap-limit, apply reformatting to comments.\n        -d | --debug          : enable debug mode. Disabled by default.\n        -e | --comma-end      : in a parameters list, end with the comma (default)\n        -f | --function-case N: Change the case of the PostgreSQL functions. Default\n                                is unchanged: 0. Values: 0=\u003eunchanged, 1=\u003elowercase,\n                                2=\u003euppercase, 3=\u003ecapitalize.\n        -F | --format STR     : output format: text or html. Default: text.\n        -g | --nogrouping     : add a newline between statements in transaction\n                                regroupement. Default is to group statements.\n        -h | --help           : show this message and exit.\n        -i | --inplace        : override input files with formatted content.\n        -k | --keep-newline   : preserve empty line in plpgsql code.\n        -L | --no-extra-line  : do not add an extra empty line at end of the output.\n        -m | --maxlength SIZE : maximum length of a query, it will be cutted above\n                                the given size. Default: no truncate.\n        -M | --multiline      : enable multi-line search for -p or --placeholder.\n        -n | --nocomment      : remove any comment from SQL code.\n        -N | --numbering      : statement numbering as a comment before each query.\n        -o | --output file    : define the filename for the output. Default: stdout.\n        -p | --placeholder RE : set regex to find code that must not be changed.\n        -r | --redshift       : add RedShift keyworks to the list of SQL keyworks.\n                                Obsolete now, use --extra-keyword 'redshift' instead.\n        -s | --spaces size    : change space indent, default 4 spaces.\n        -S | --separator STR  : dynamic code separator, default to single quote.\n        -t | --format-type    : try another formatting type for some statements.\n        -T | --tabs           : use tabs instead of space characters, when used\n                                spaces is set to 1 whatever is the value set to -s.\n        -u | --keyword-case N : Change the case of the reserved keyword. Default is\n                                uppercase: 2. Values: 0=\u003eunchanged, 1=\u003elowercase,\n                                2=\u003euppercase, 3=\u003ecapitalize.\n        -U | --type-case N    : Change the case of the data type name. Default is\n                                lowercase: 1. Values: 0=\u003eunchanged, 1=\u003elowercase,\n                                2=\u003euppercase, 3=\u003ecapitalize.\n        -v | --version        : show pg_format version and exit.\n        -w | --wrap-limit N   : wrap queries at a certain length.\n        -W | --wrap-after N   : number of column after which lists must be wrapped.\n                                Default: puts every item on its own line.\n        -X | --no-rcfile      : don't read rc files automatically (./.pg_format or\n                                $HOME/.pg_format or $XDG_CONFIG_HOME/pg_format).\n                                The --config / -c option overrides it.\n        --extra-function FILE : file containing a list of functions to use the same\n                                formatting as PostgreSQL internal function.\n        --extra-keyword FILE  : file containing a list of keywords to use the same\n                                formatting as PostgreSQL internal keyword. Use\n                                special value 'redshift' for support to Redshift\n                                keywords defined internaly in pgFormatter.\n        --no-space-function : remove space between function call and the open\n                                parenthesis.\n        --redundant-parenthesis: do not remove redundant parenthesis in DML.\n\n    Examples:\n\n        cat samples/ex1.sql | pg_format -\n        pg_format -n samples/ex1.sql\n        pg_format -f 2 -n -o result.sql samples/ex1.sql\n\n  CGI context\n    Install pg_format into your cgi-bin folder, grant execution on it as a\n    CGI script (maybe you should add the .cgi extension) and get it from\n    your favorite browser. Copy files logo_pgformatter.png and\n    icon_pgformatter.ico in the CGI directory, pg_format.cgi look for them\n    in the same repository.\n\n    You have a live example without limitation than ten thousand characters\n    in your SQL query here:\n\n            http://sqlformat.darold.net/\n\n    pg_format will automatically detected that it is running in a CGI\n    environment and will output all html code needed to run an online code\n    formatter site. There's nothing more to do.\n\n    You need to install the Perl CGI and JSON modules first. If it is not\n    already the case do:\n\n            yum install perl-cgi\n            yum install perl-json\n    or\n            apt install libcgi-pm-perl\n            apt install libjson-perl\n\n    following your distribution.\n\nINSTALLATION\n    Following your Linux distribution you might need to install the autodie\n    Perl module:\n\n            sudo yum -y install perl-autodie\n\n    Download the tarball from github and unpack the archive as follow:\n\n            version=5.3 #please use the latest release version from github\n            wget https://github.com/darold/pgFormatter/archive/refs/tags/v${version}.tar.gz\n            tar xzf v${version}.tar.gz\n            cd pgFormatter-${version}/\n            perl Makefile.PL\n            make \u0026\u0026 sudo make install\n            cd ../ \u0026\u0026 rm -rf v${version}.tar.gz \u0026\u0026 rm -rf pgFormatter-${version} #clean up\n\n    This will copy the Perl script pg_format in /usr/local/bin/pg_format\n    directory by default and the man page into\n    /usr/local/share/man/man1/pg_format.1. Those are the default\n    installation directory for 'site' install.\n\n    If you want to install all under /usr/ location, use INSTALLDIRS='perl'\n    as argument of Makefile.PL. The script will be installed into\n    /usr/bin/pg_format and the manpage into /usr/share/man/man1/pg_format.1.\n\n    For example, to install everything just like Debian does, proceed as\n    follow:\n\n            perl Makefile.PL INSTALLDIRS=vendor\n\n    By default INSTALLDIRS is set to site.\n\n    Regression tests can be executed with the following command:\n\n            make test\n\n    If you have docker installed you can build a pgFormatter image using:\n\n            docker build -t darold.net/pgformatter .\n\n    then just use it as\n\n            cat file.sql | docker run --rm -a stdin -a stdout -i darold.net/pgformatter -\n\nSPECIAL FORMATTING\n  Option -W, --wrap-after\n    This option can be used to set number of column after which lists must\n    be wrapped. By default pgFormatter puts every item on its own line. This\n    format applies to SELECT and FROM list. For example the following query:\n\n        SELECT a, b, c, d FROM t_1, t_2, t3 WHERE a = 10 AND b = 10;\n\n    will be formatted into with -W 4:\n\n        SELECT a, b, c, d\n        FROM t_1, t_2, t3\n        WHERE a = 10\n            AND b = 10;\n\n    Note this formatting doesn't fits well with sub queries in list.\n\n  Option -w, --wrap-limit\n    This option wraps queries at a certain length whatever is the part of\n    the query at the limit unless it is a comment. For example if the limit\n    is reach in a text constant the text will be wrapped. Indentation is not\n    included in the character count. This option is applied in all cases\n    even if other options are used.\n\n  Option -C, --wrap-comment\n    This option wraps comments at the length defined by -w, --wrap-limit\n    whatever is the part of the comment. Indentation is not included in the\n    character count.\n\n  Option -t, --format-type\n    This option activate an alternative formatting that adds:\n\n      * newline in procedure/function parameter list\n      * new line in PUBLICATION and POLICY DDL\n      * keep enumeration in GROUP BY clause on a single line\n\n    Expect this list grow following alternative thoughts.\n\n  Option -g, --nogrouping\n    By default pgFormatter groups all statements when they are in a\n    transaction:\n\n        BEGIN;\n        INSERT INTO foo VALUES (1, 'text 1');\n        INSERT INTO foo VALUES (2, 'text 2');\n        ...\n        COMMIT;\n\n    By disabling grouping of statement pgFormatter will always add an extra\n    newline characters between statements just like outside a transaction:\n\n        BEGIN;\n\n        INSERT INTO foo VALUES (1, 'text 1');\n\n        INSERT INTO foo VALUES (2, 'text 2');\n        ...\n\n        COMMIT;\n\n    This might add readability to not DML transactions.\n\n  Option -L, --no-extra-line\n    By default pgFormatter always adds an empty line after the end of a\n    statement when it is terminated by a ; except in a plpgsql code block.\n    If the extra empty line at end of the output is useless, you can remove\n    it by adding this option to the command line.\n\n  Option --extra-function\n    pgFormatter applies some formatting to the PostgreSQL internal functions\n    call but it can not detect user defined function. It is possible to\n    defined a list of functions into a file (one function name per line) and\n    give it to pgFormatter through the --extra-function option that will be\n    formatter as PostgreSQL internal functions.\n\n  Option --extra-keyword\n    pgFormatter applies some formatting to the PostgreSQL internal keywords\n    but it can not detect keywords for other database. It is possible to\n    defined a list of keywords into a file (one keyword per line) and give\n    it to pgFormatter through the --extra-keyword option that will be\n    formatter as PostgreSQL internal functions.\n\n    You can also pass a special value 'redshift' that will load the keywords\n    defined internally in pgFormatter for this database. This was\n    historically possible through the -r | --redshift option that is now\n    obsolete and will be removed in the future.\n\n  Option --no-space-function\n    Use this option to remove the space character between a function call\n    and the open parenthesis that follow. By default pgFormatter adds a\n    space character, for example:\n\n        DROP FUNCTION IF EXISTS app_public.hello (a text);\n\n    When this option is used the resulting query is formatted as follow:\n\n        DROP FUNCTION IF EXISTS app_public.hello(a text);\n\n  Option --redundant-parenthesis\n    By default, pgFormatter tries to remove redundant parenthesis in DML but\n    in some cases they must be preseved. Using this option will keep\n    redundant parenthesis untouched.\n\nHINTS\n  Configuration\n    If the default settings of pg_format doesn't fit all your needs you can\n    customize the behavior of pg_format by using a configuration file\n    instead of repeating the command line option. By default pgFormatter\n    look for files ./.pg_format or $HOME/.pg_format or\n    $XDG_CONFIG_HOME/pg_format/pg_format.conf if they exists but you can\n    choose an alternate configuration file using command line option -c |\n    --config\n\n    To customize the CGI pg_format.cgi look for a configuration file named\n    pg_format.conf in the same directory as the CGI script.\n\n    For a sample configuration file see doc/pg_format.conf.sample\n\n    To prevent pg_format to look at $XDG_CONFIG_HOME/pg_format or\n    $HOME/.pg_format files you can use the command line option -X |\n    --no-rcfile\n\n  Formatting from stdin\n    You can execute pg_format without any argument or - to give the SQL code\n    to format through stdin.\n\n    If you use the interactive mode you have to type `ctrl+d` after typing\n    your SQL statement to format to end the typing.\n\n            $ pg_format\n            select * from customers;\n            \u003c ctrl+d \u003e\n\n    You can use stdin in a one liner as follow:\n\n            echo \"select * from customers;\" | pg_format\n\n  Formatting from VI\n    With pgFormatter, you can just add the following line to your ~/.vimrc\n    file:\n\n            au FileType sql setl formatprg=/usr/local/bin/pg_format\\ -\n\n    This lets your gq commands use pgFormatter automagically. For example if\n    you are on the first line, typing:\n\n            ESC+gq+G\n\n    will format the entire file.\n\n            ESC+gq+2j\n\n    will format the next two line.\n\n    Thanks to David Fetter for the hint.\n\n    There is also the (Neo)vim plugin for formatting code for many file\n    types that support pg_format to format SQL file type. Thanks to Anders\n    Riutta for the patch to (Neo)vim.\n\n  Formatting from Atom\n    If you use atom as your favorite editor you can install the pg-formatter\n    package which is a Node.js wrapper of pgFormatter.\n\n    Features:\n\n      * Format selected text or a whole file via keyboard shortcut or command.\n      * Format SQL files on save.\n\n    Installation:\n\n    Search for pg-formatter in Atom UI or get it via command line:\n\n        apm install pg-formatter\n\n    Usage:\n\n    Hit Ctrl-Alt-F to format selected text (or a whole file) or define your\n    shortcut:\n\n        'ctrl-alt-p': 'pg-formatter:format'\n\n    Also, you can automatically format SQL files on save (disabled by\n    default).\n\n    You can download the package from url:\n\n            https://atom.io/packages/pg-formatter\n\n    the sources are available at https://github.com/gajus/pg-formatter\n\n    Thanks to Alex Fedoseev for the atom package.\n\n  Formatting from Visual Studio\n    Thanks to Brady Holt a Visual Studio Code extension is available to\n    formats PostgresSQL SQL using pgFormatter.\n\n            https://marketplace.visualstudio.com/items?itemName=bradymholt.pgformatter\n\n    For installation and use have a look at URL above.\n\n  Prevent replacing code snippets\n    Using -p or --placeholder command line option it is possible to keep\n    code untouched by pgFormatter in your SQL queries. For example, in query\n    like:\n\n            SELECT * FROM projects WHERE projectnumber\n                    IN \u003c\u003cinternalprojects\u003e\u003e AND username = \u003c\u003cloginname\u003e\u003e;\n\n    you may want pgFormatter to not interpret \u003c\u003c and \u003e\u003e as bit-shift\n    keywords and modify your code snippets. You can use a Perl regular\n    expression to instruct pgFormatter to keep some part of the query\n    untouched. For example:\n\n            pg_format samples/ex9.sql -p '\u003c\u003c(?:.*)?\u003e\u003e'\n\n    will not format the bit-shift like operators.\n\n    If you would like to wrap queries after 60 characters (-w 60) and to\n    apply that limit to comments as well (-C), then urls in comments may get\n    wrapped. If you would prefer not to wrap urls, you can use a regular\n    expression to avoid wrapping urls. For example:\n\n            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()@:%_\\+.~#?\u0026//=]*)'\n\n    will wrap the queries and the comments, but not the urls.\n\n  Prevent dynamic code formatting\n    By default pgFormatter takes all code between single quote as string\n    constant and do not perform any formatting on this code. It is common to\n    use a string as code separator to avoid doubling single quote in dynamic\n    code generation, in this case pgFormatter can fail to auto detect the\n    code separator. By default it will search for any string after the\n    EXECUTE keyword starting with dollar sign. If it can not auto detect\n    your code separator you can use the command line option -S or\n    --separator to set the code separator that must be used.\n\n  Node.js thin-wrapper\n    Gajus Kuizinas has written a Node.js wrapper for executing pgFormatter.\n    You can find it at https://github.com/gajus/pg-formatter\n\n  Customize CSS for the CGI output\n    You can change the HTML style rendered through the default CSS style by\n    creating a file named custom_css_file.css into the pgFormatter CGI\n    script directory. The default CSS will be fully overridden by this\n    custom file content. You have to look at the generated HTML output to\n    get the default CSS code used.\n\n  Using pgFormatter as an API\n    You may use pgFormatter as an API by setting the 'Accept' HTTP header to\n    value 'application/json' when calling it as a CGI app. In case you do\n    not want to enable this feature, set \"$self-\u003e{ 'enable_api' } = 0\" in\n    the \"set_config\" sub of lib/pgFormatter/CGI.pm.\n\nAUTHORS\n    pgFormatter is an original work from Gilles Darold with major code\n    refactoring by Hubert depesz Lubaczewski.\n\nCOPYRIGHT\n    Copyright 2012-2025 Gilles Darold. All rights reserved.\n\nLICENSE\n    pgFormatter is free software distributed under the PostgreSQL Licence.\n\n    A modified version of the SQL::Beautify Perl Module is embedded in\n    pgFormatter with copyright (C) 2009 by Jonas Kramer and is published\n    under the terms of the Artistic License 2.0.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarold%2Fpgformatter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdarold%2Fpgformatter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarold%2Fpgformatter/lists"}