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.

1

5 Answers 5

10

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.

2
  • 1
    Sorry i think you misunderstood the question, I can format this myself. What I'm looking for is something to do this in a command or IDE. Example: I would highlight the query and press ctrl + shift + F, this would auto indent, all keyword to UPPER, each command/keyword starts on a new line, etc... So if someone type or scripted this long query I just would like a quick way to format it instead of doing this by hand. Here is a online example: dpriver.com/pp/sqlformat.htm Commented Jan 23, 2012 at 18:54
  • 1
    @PhillPafford: Yeah, not exactly what you are looking for. The closest thing to a PostgreSQL query formatter I know. Doesn't mean there isn't anything out there. Commented Jan 23, 2012 at 19:15
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:~] 
$ 
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

1

Online SQL beautifier1 AND beautifier2

-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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.