The already included SQL query builder seems to be a bit complex when all you need to accomplish is to run a single query. Because of that reason I added the QGIS plugin Fast SQL Layer, which lets you write your query in a simple console.
What is Fast SQL Layer?
Fast SQL Layer is a QGIS plugin written by Pablo T. Carreira to run SQL queries for PostGIS and SpatiaLite data. It also comes with its own SQL syntax highlighter. I have edited it a bit for integrating it to the PostGIS viewer.
Python, PyQt4, PyQGIS, libqt4-sql-psql, psycopg2 and pygments.
How to install prerequisites in Windows: See this post, you will also need to install psycopg2 and pygments from OSGeo4W.
How to install prerequisites in Ubuntu/Linux: For QGIS, see downloads official page. For the other programs/libs use apt-get or Synaptic.
Once you have the prerequisites installed, all you need to do is to extract one ZIP file (download the zip file here) into the appropriate path (on Windows: C:/Archivos de programa/PostgreSQL/8.4/bin/ whereas on Ubuntu/Linux: /usr/bin/) as well as edit the pgAdmin plugins.ini file.
On Ubuntu/Linux you will need the sudo command to extract and set the 755 permissions to the extracted files, this way:
Additionally, copy the next lines to /usr/share/pgadmin3/plugins.ini (if the file does not exist, you have to create it):
On Windows edit C:/Archivos de programa/PostgreSQL/8.4/pgAdmin III/plugins.ini adding the following lines:
The postgis_viewer.bat file sets the OSGeo4W environment variables (Qt, Python, QGIS) for allowing the execution of postgis_viewer.py The .bat file assumes OSGeo4W is installed in C:/ and PostgreSQL can be found in C:/Archivos de programa/PostgreSQL/8.4/ You should check that file if you get some troubles while running the plugin.
How can I use it?
You can use the PostGIS viewer from a command line (like in this post) as well as from pgAdmin. If the installation process went well, to see one of your spatial tables you need to select it in the pgAdmin's object browser, click in Plugins and then click in View PostGIS layer, this way:
Now you can write your SQL queries in the Query tab. Don't forget to provide an id field name and a geometry field name to get a well defined (valid) layer. If you get a new layer from your query but cannot see any geometry on the map, you probably need to adjust the id field name.