{{Outdated}}
Note: Text for this page kindly written up by Chris Yesson

Note2: The instructions below will no longer be necessary for versions 0.7 and later. These versions automatically find views with geometry fields and in most cases will find an 'oid' type column too. See the QGIS 0.7 or later manual for further information.

This is really a Postgres/GIS based fix rather than a QGIS one. All you need to do is put an extra column in your view and then log the table in the right place. Details below.

  • Your view must include the OID of the table containing the geometry field. - It must also contain the geometry field which has been named using the AS syntax.
  • You must add your view to the geometry_columns table as QGIS takes this to be the list of geo-enabled tables, you cannot use the postgis function AddGeometryColumn as this will try to add a column to your view. You have to do your own insert, but to do this just copy the entry for your geo-enabled table and change the table-name to your view name.

The view should then be visible from QGIS when you connect to the postgres database.

An example (tbl_foobar is a geo-enabled table)

 -- create the view
 CREATE OR REPLACE VIEW view_foobar AS
 SELECT
 tbl_foobar.oid AS oid,            -- oid field 
 tbl_foobar.the_geom AS the_geom,  -- geometry column
 ...                             -- random other stuff
 FROM tbl_foobar;
 -- select * from geometry_columns ;
 -- f_table_catalog | f_table_schema | f_table_name | f_geometry_column | coord_dimension | srid | type | attrelid | varattnum | stats
 -- | public | tbl_foobar | the_geom | 2 |   -1 | MULTIPOLYGON |   352872 |        15 |
 -- copy the data and change the table-name for the view name
 INSERT INTO geometry_columns (
 f_table_catalog, f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, "type",  attrelid, varattnum, stats) 
 VALUES  (_*, 'public', 'view_foobar', 'the_geom', 2, -1, 'MULTIPOLYGON', 352872, 15, NULL);