I am attempting to create a QgsVectorLayer with the same code as I am using for a PostgreSQL table, which works correctly and the isValid() method on the layer returns true.

print('sv_demog layer is valid: %s' % (vlayer.isValid()))

The view selects from table where id is the primary key, and has a geometry column.

How do I make this work for a view?

I am using a PostgreSQL 10 database, and QGIS 3 (Update: Same behavior in 2.18).

Ref: Loading Layers

1 Answer 1


Jay, I had the same problem. You have to specify the view's primary key. For example, uri.setDataSource('my schema', 'my_view', 'geom', '', 'id')

Check out this thread postGIS views, qGIS, qGIS python console

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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