Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Under Qgis 1.8 on Windows 7 I have this problem with my pyArchInit plugin: I need to load on the map canvas my geometries from a postgis layer. Till the 1.7.4 version I use the method explained on the tutorial.

But I have to load also some potgres view, and Qgis 1.8 need to know the column that I would like to use as ID.

How can I pass this value with pyQgis and restore my plugin?

Thanks a lot for any help!

uri = QgsDataSourceURI()
# set host name, port, database name, username and password
uri.setConnection("localhost", "5432", "dbname", "johny", "xxx")
# set database schema, table name, geometry column and optionaly subset (WHERE clause)
uri.setDataSource("public", "roads", "the_geom", "cityid = 2643")

vlayer = QgsVectorLayer(uri.uri(), "layer_name_you_like", "postgres")
share|improve this question

QgsDataSourceURI.setDataSource() has an additional fifth argument to specify the key column.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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