Fast SQL Layer for QGIS

For everyone working with spatial databases in QGIS there comes a time when “Add PostGIS/SpatiaLite Layer” and “RT Sql Layer” start to be annoying. You always have to retype or copy-paste your SQL queries into the user interface if you need to change the tiniest thing in the layer’s definition.

This is where “Fast SQL Layer” can be a real time saver. Fast SQL Layer is a new plugin for QGIS by Pablo T. Carreira. It basically adds an SQL console for loading layers from PostGIS/SpatiaLite into QGIS. And it even comes with syntax highlighting!


Fast SQL Layer comes with one dependency: Pygments, which is used for syntax highlighting.

On Ubuntu, all you have to do is install it with apt-get:

sudo apt-get install python-pygments

For Windows with OSGeo4W, @Mike_Toews posted this on gis.stackexchange:

I downloaded and extracted Pygments-1.4.tar.gz, then in an OSGeo4W shell within the Pygments-1.4 directory, type python build then python install


When you activate the plugin in plugin manager, a dock widget will appear which contains the console and some fields for specifying the database connection that should be used. Then, you can simply write your SQL query and load the results with one click.

Fast SQL plugin

In this example, I renamed “gid” to “id”, but you can actually edit the values in the drop down boxes to adjust the column names for id and geometry:

A second layer loaded using Fast SQL plugin

It certainly needs some polishing on the user interface side but I really like it.

  1. Pablo Torres Carreira said:

    Nice post, that will certainly help people use/install the Fast SQL plugin. Regards, Pablo.

  2. Hi,
    thanks for this fantastic blog. So much useful information!

    I was trying to install the FAST SQL plugin on Quantum GIS 1.7 on Ubuntu (from OSGEO live DVD, virtual machine).

    When FAST SQL is activated I get this error on startup:

    Couldn’t load plugin FastSQLlayer due an error when calling its initGui() method

    Traceback (most recent call last):
    File “/usr/lib/python2.6/dist-packages/qgis/”, line 147, in startPlugin
    File “/home/user/.qgis/python/plugins/FastSQLlayer/”, line 59, in initGui
    actions = conn.getAvailableConnections()
    File “/home/user/.qgis/python/plugins/FastSQLlayer/”, line 48, in getAvailableConnections
    connection = self.getConnection( c )
    File “/home/user/.qgis/python/plugins/FastSQLlayer/”, line 32, in getConnection
    exec( “from connectors import %s as connector” % conntype)
    File “”, line 1, in
    File “/usr/lib/python2.6/dist-packages/qgis/”, line 283, in _import
    mod = _builtin_import(name, globals, locals, fromlist, level)
    File “/home/user/.qgis/python/plugins/FastSQLlayer/connectors/”, line 18, in
    from pyspatialite import dbapi2 as sqlite
    File “/usr/lib/python2.6/dist-packages/qgis/”, line 283, in _import
    mod = _builtin_import(name, globals, locals, fromlist, level)
    ImportError: No module named pyspatialite

    Python version:
    2.6.5 (r265:79063, Apr 16 2010, 13:28:26)
    [GCC 4.4.3]

    QGIS version:
    1.7.0-Wroclaw Wroclaw, exported

    Python path: [‘/usr/share/qgis/python’, ‘/home/user/.qgis/python’, ‘/home/user/.qgis/python/plugins’, ‘/usr/share/qgis/python/plugins’, ‘/usr/local/lib/python2.6/dist-packages/Geraldo-0.4.10_stable-py2.6.egg’, ‘/usr/local/lib/python2.6/dist-packages/OWSLib-0.3.1-py2.6.egg’, ‘/usr/lib/python2.6’, ‘/usr/lib/python2.6/plat-linux2’, ‘/usr/lib/python2.6/lib-tk’, ‘/usr/lib/python2.6/lib-old’, ‘/usr/lib/python2.6/lib-dynload’, ‘/usr/lib/python2.6/dist-packages’, ‘/usr/lib/python2.6/dist-packages/PIL’, ‘/usr/lib/python2.6/dist-packages/gst-0.10’, ‘/usr/lib/pymodules/python2.6’, ‘/usr/lib/python2.6/dist-packages/gtk-2.0’, ‘/usr/lib/pymodules/python2.6/gtk-2.0’, ‘/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode’, ‘/usr/local/lib/python2.6/dist-packages’, ‘/usr/local/lib/python2.6/dist-packages/setuptools-0.6c11-py2.6.egg-info’, ‘/home/user/.qgis/python/plugins/mmqgis/forms’, ‘.’]

    Any idea why?

    Best regards


    • underdark said:

      ImportError: No module named pyspatialite

      This means that you are missing the pyspatialite package on your system. You’ll have to install it before the plugin can work.

  3. Thanks a lot. I should have figured that out my self.

    I can add that I also was required to install libgeos 3.2.0


  4. alex said:

    hi all,
    You can also have a look at QSpatiaLite plugin for managing SpatiaLite within QGIS in a very user friendly way,

%d bloggers like this: