1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
|
=head1 NAME
pg_format - PostgreSQL SQL syntax beautifier
=head1 DESCRIPTION
This SQL formatter/beautifier supports keywords from SQL-92, SQL-99,
SQL-2003, SQL-2008, SQL-2011 and PostgreSQL specifics keywords. Works
with any other databases too.
pgFormatter can work as a console program or as a CGI. It will automatically
detect its environment and format output as text or as HTML following the context.
It can also return a JSON-formatted response if used as CGI with 'Accept: application/json'.
Keywords highlighting will only be available in CGI context.
=head2 Terminal/console execution
Usage: pg_format [options] file.sql
PostgreSQL SQL queries and PL/PGSQL code beautifier.
Arguments:
file.sql can be a file, multiple files or use - to read query from stdin.
Returning the SQL formatted to stdout or into a file specified with
the -o | --output option.
Options:
-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 keyworks to the list of SQL keyworks.
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 is 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.
Examples:
cat samples/ex1.sql | pg_format -
pg_format -n samples/ex1.sql
pg_format -f 2 -n -o result.sql samples/ex1.sql
=head2 CGI context
Install pg_format into your cgi-bin folder, grant execution on it as a CGI
script (maybe you should add the .cgi extension) and get it from your favorite
browser. Copy files logo_pgformatter.png and icon_pgformatter.ico in the CGI
directory, pg_format.cgi look for them in the same repository.
You have a live example without limitation than ten thousand characters in
your SQL query here:
http://sqlformat.darold.net/
pg_format will automatically detected that it is running in a CGI environment
and will output all html code needed to run an online code formatter site.
There's nothing more to do.
You need to install the Perl CGI and JSON modules first. If it is not already
the case do:
yum install perl-cgi
yum install perl-json
or
apt install libcgi-pm-perl
apt install libjson-perl
following your distribution.
=head1 INSTALLATION
Following your Linux distribution you might need to install the autodie Perl module:
sudo yum -y install perl-autodie
Download the tarball from github and unpack the archive as follow:
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
cd ../ && rm -rf v${version}.tar.gz && rm -rf pgFormatter-${version} #clean up
This will copy the Perl script pg_format in /usr/local/bin/pg_format directory
by default and the man page into /usr/local/share/man/man1/pg_format.1. Those are
the default installation directory for 'site' install.
If you want to install all under /usr/ location, use INSTALLDIRS='perl' as argument
of Makefile.PL. The script will be installed into /usr/bin/pg_format and the
manpage into /usr/share/man/man1/pg_format.1.
For example, to install everything just like Debian does, proceed as follow:
perl Makefile.PL INSTALLDIRS=vendor
By default INSTALLDIRS is set to site.
Regression tests can be executed with the following command:
make test
If you have docker installed you can build a pgFormatter image using:
docker build -t darold.net/pgformatter .
then just use it as
cat file.sql | docker run --rm -a stdin -a stdout -i darold.net/pgformatter -
=head1 SPECIAL FORMATTING
=head2 Option -W, --wrap-after
This option can be used to set number of column after which lists must be
wrapped. By default pgFormatter puts every item on its own line. This format
applies to SELECT and FROM list. For example the following query:
SELECT a, b, c, d FROM t_1, t_2, t3 WHERE a = 10 AND b = 10;
will be formatted into with -W 4:
SELECT a, b, c, d
FROM t_1, t_2, t3
WHERE a = 10
AND b = 10;
Note this formatting doesn't fits well with sub queries in list.
=head2 Option -w, --wrap-limit
This option wraps queries at a certain length whatever is the part
of the query at the limit unless it is a comment. For example if
the limit is reach in a text constant the text will be wrapped.
Indentation is not included in the character count. This option
is applied in all cases even if other options are used.
=head2 Option -C, --wrap-comment
This option wraps comments at the length defined by -w, --wrap-limit
whatever is the part of the comment. Indentation is not included in
the character count.
=head2 Option -t, --format-type
This option activate an alternative formatting that adds:
* newline in procedure/function parameter list
* new line in PUBLICATION and POLICY DDL
* keep enumeration in GROUP BY clause on a single line
Expect this list grow following alternative thoughts.
=head2 Option -g, --nogrouping
By default pgFormatter groups all statements when they are in a
transaction:
BEGIN;
INSERT INTO foo VALUES (1, 'text 1');
INSERT INTO foo VALUES (2, 'text 2');
...
COMMIT;
By disabling grouping of statement pgFormatter will always add an
extra newline characters between statements just like outside a
transaction:
BEGIN;
INSERT INTO foo VALUES (1, 'text 1');
INSERT INTO foo VALUES (2, 'text 2');
...
COMMIT;
This might add readability to not DML transactions.
=head2 Option -L, --no-extra-line
By default pgFormatter always adds an empty line after the end of a
statement when it is terminated by a ; except in a plpgsql code block.
If the extra empty line at end of the output is useless, you can remove
it by adding this option to the command line.
=head2 Option --extra-function
pgFormatter applies some formatting to the PostgreSQL internal functions call
but it can not detect user defined function. It is possible to defined a list
of functions into a file (one function name per line) and give it to pgFormatter
through the --extra-function option that will be formatter as PostgreSQL
internal functions.
=head2 Option --extra-keyword
pgFormatter applies some formatting to the PostgreSQL internal keywords but it
can not detect keywords for other database. It is possible to defined a list
of keywords into a file (one keyword per line) and give it to pgFormatter
through the --extra-keyword option that will be formatter as PostgreSQL
internal functions.
You can also pass a special value 'redshift' that will load the keywords
defined internally in pgFormatter for this database. This was historically
possible through the -r | --redshift option that is now obsolete and will
be removed in the future.
=head2 Option --no-space-function
Use this option to remove the space character between a function call
and the open parenthesis that follow. By default pgFormatter adds a space
character, for example:
DROP FUNCTION IF EXISTS app_public.hello (a text);
When this option is used the resulting query is formatted as follow:
DROP FUNCTION IF EXISTS app_public.hello(a text);
=head2 Option --redundant-parenthesis
By default, pgFormatter tries to remove redundant parenthesis in DML but in
some cases they must be preseved. Using this option will keep redundant
parenthesis untouched.
=head1 HINTS
=head2 Configuration
If the default settings of pg_format doesn't fit all your needs you can
customize the behavior of pg_format by using a configuration file instead
of repeating the command line option. By default pgFormatter look for files
./.pg_format or $HOME/.pg_format or $XDG_CONFIG_HOME/pg_format/pg_format.conf
if they exists but you can choose an alternate configuration file using command
line option -c | --config
To customize the CGI pg_format.cgi look for a configuration file named
pg_format.conf in the same directory as the CGI script.
For a sample configuration file see doc/pg_format.conf.sample
To prevent pg_format to look at $XDG_CONFIG_HOME/pg_format or $HOME/.pg_format
files you can use the command line option -X | --no-rcfile
=head2 Formatting from stdin
You can execute pg_format without any argument or - to give the SQL code to
format through stdin.
If you use the interactive mode you have to type `ctrl+d` after typing your
SQL statement to format to end the typing.
$ pg_format
select * from customers;
< ctrl+d >
You can use stdin in a one liner as follow:
echo "select * from customers;" | pg_format
=head2 Formatting from VI
With pgFormatter, you can just add the following line to your ~/.vimrc file:
au FileType sql setl formatprg=/usr/local/bin/pg_format\ -
This lets your gq commands use pgFormatter automagically. For example if you
are on the first line, typing:
ESC+gq+G
will format the entire file.
ESC+gq+2j
will format the next two line.
Thanks to David Fetter for the hint.
There is also the (Neo)vim plugin for formatting code for many file types
that support pg_format to format SQL file type. Thanks to Anders Riutta
for the patch to (Neo)vim.
=head2 Formatting from Atom
If you use atom as your favorite editor you can install the pg-formatter
package which is a Node.js wrapper of pgFormatter.
Features:
* Format selected text or a whole file via keyboard shortcut or command.
* Format SQL files on save.
Installation:
Search for pg-formatter in Atom UI or get it via command line:
apm install pg-formatter
Usage:
Hit Ctrl-Alt-F to format selected text (or a whole file) or define
your shortcut:
'ctrl-alt-p': 'pg-formatter:format'
Also, you can automatically format SQL files on save (disabled by default).
You can download the package from url:
https://atom.io/packages/pg-formatter
the sources are available at https://github.com/gajus/pg-formatter
Thanks to Alex Fedoseev for the atom package.
=head2 Formatting from Visual Studio
Thanks to Brady Holt a Visual Studio Code extension is available to formats
PostgresSQL SQL using pgFormatter.
https://marketplace.visualstudio.com/items?itemName=bradymholt.pgformatter
For installation and use have a look at URL above.
=head2 Prevent replacing code snippets
Using -p or --placeholder command line option it is possible to keep code
untouched by pgFormatter in your SQL queries. For example, in query like:
SELECT * FROM projects WHERE projectnumber
IN <<internalprojects>> AND username = <<loginname>>;
you may want pgFormatter to not interpret << and >> as bit-shift keywords
and modify your code snippets. You can use a Perl regular expression to
instruct pgFormatter to keep some part of the query untouched. For example:
pg_format samples/ex9.sql -p '<<(?:.*)?>>'
will not format the bit-shift like operators.
If you would like to wrap queries after 60 characters (-w 60) and to
apply that limit to comments as well (-C), then urls in comments may get
wrapped. If you would prefer not to wrap urls, you can use a regular
expression to avoid wrapping urls. For example:
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()@:%_\+.~#?&//=]*)'
will wrap the queries and the comments, but not the urls.
=head2 Prevent dynamic code formatting
By default pgFormatter takes all code between single quote as string constant
and do not perform any formatting on this code. It is common to use a string
as code separator to avoid doubling single quote in dynamic code generation,
in this case pgFormatter can fail to auto detect the code separator. By
default it will search for any string after the EXECUTE keyword starting with
dollar sign. If it can not auto detect your code separator you can use the
command line option -S or --separator to set the code separator that must be
used.
=head2 Node.js thin-wrapper
Gajus Kuizinas has written a Node.js wrapper for executing pgFormatter.
You can find it at https://github.com/gajus/pg-formatter
=head2 Customize CSS for the CGI output
You can change the HTML style rendered through the default CSS style by
creating a file named custom_css_file.css into the pgFormatter CGI script
directory. The default CSS will be fully overridden by this custom file
content. You have to look at the generated HTML output to get the default
CSS code used.
=head2 Using pgFormatter as an API
You may use pgFormatter as an API by setting the 'Accept' HTTP header to
value 'application/json' when calling it as a CGI app. In case you do not
want to enable this feature, set "$self->{ 'enable_api' } = 0" in the
"set_config" sub of lib/pgFormatter/CGI.pm.
=head1 AUTHORS
pgFormatter is an original work from Gilles Darold with major code refactoring
by Hubert depesz Lubaczewski.
=head1 COPYRIGHT
Copyright 2012-2025 Gilles Darold. All rights reserved.
=head1 LICENSE
pgFormatter is free software distributed under the PostgreSQL Licence.
A modified version of the SQL::Beautify Perl Module is embedded in pgFormatter
with copyright (C) 2009 by Jonas Kramer and is published under the terms of
the Artistic License 2.0.
|