16

Are there any (Linux based) SQL Query Formatting programs/plugins/extensions?

I use PostgreSQL and MySQL but other DB's are welcome as well.

I can use a VM to test with but would prefer a Linux (Ubuntu) based solution.

I have seen a online version but nothing as a installable.

Eclipse Based IDE's are a plus as well

Example:

select f1, f2, fname, lName from tblName where f1 = true and fname is not null order by lName asc

to something like

SELECT f1, f2, fname, lName
FROM tblName
WHERE f1 = true
AND fname IS NOT NULL
ORDER BY lName ASC

Here is a online example:

But I would rather this be in a local environment

Related:

UPDATE:

Looking at this:

FINAL UPDATE:

While this might be an overkill JetBrains has a database IDE, DataGrip, which has some great re-formatting options.

Timur Shtatland
  • 125
  • 2
  • 11
Phill Pafford
  • 1,415
  • 6
  • 19
  • 26

5 Answers5

11

Update

Modern versions of pgAdmin4 include formatting on demand in the query tool with Ctrl + K.
Formatting can be customized in the preferences in a limited way.


Original answer

pgAdmin comes with a built in query formatter. You would create a view to see the query formatted like this:

pgadmin3 SQL editor with formatted view

Similar in pgAdmin4.

Of course, unquoted mixed case identifiers will be cast to lower case. (But I would consider this a blessing.)

And noise words like ASC in ORDER BY will be trimmed.

And last but not least, the view would have to be valid within the database to begin with. So not exactly what you asked for. Queries are actually reverse engineered from the view definition.

Erwin Brandstetter
  • 185,527
  • 28
  • 463
  • 633
3

While more generic (SQL instead of pgSQL), fsqlf (http://sourceforge.net/projects/fsqlf/) is a command line or GUI program, open source, to format SQL. It supports having a formatting.conf file which allows you a lot of flexibility in how the final product looks.

Examples:

☺  [wwalker@speedy:~] 
$ echo "select f1, f2, fname, lName from tblName where f1 = true and fname is not null order by lName asc" | fsqlf 

SELECT
  f1
, f2
, fname
, lName
FROM tblName
WHERE f1=true
AND fname is not null
ORDER BY lName asc
☺  [wwalker@speedy:~] 
$ vim formatting.conf # 2 character change
☺  [wwalker@speedy:~] 
$ echo "select f1, f2, fname, lName from tblName where f1 = true and fname is not null order by lName asc" | fsqlf 

SELECT
 f1 ,
 f2 ,
 fname ,
 lName
FROM tblName
WHERE f1=true
AND fname is not null
ORDER BY lName asc
☺  [wwalker@speedy:~] 
$ vim formatting.conf # 1 character change
☺  [wwalker@speedy:~] 
$ echo "select f1, f2, fname, lName from tblName where f1 = true and fname is not null order by lName asc" | fsqlf 

SELECT
 f1 , f2 , fname , lName
FROM tblName
WHERE f1=true
AND fname is not null
ORDER BY lName asc
☺  [wwalker@speedy:~] 
$ 
Wayne Walker
  • 141
  • 2
1

MySQL Workbench's query editor natively provides a SQL Query Formatter: you just need to click on the "beautify" icon as shown below (red circle). Note that it doesn't change the case of the SQL operators. (I submitted a bug report.)

enter image description here

Franck Dernoncourt
  • 2,083
  • 13
  • 34
  • 52
1

Online SQL beautifier1 AND beautifier2

Faruk Omar
  • 119
  • 2
-2

SQLinForm (http://www.sqlinform.com) is based upon Java and thus can run in Linux environment. They have an API and command line version which you could easily use in your scripts.

Guido
  • 95
  • 1