2

I am trying to load a PostGIS view public.view1 into QGIS (3.6) as follows (per Add PostGIS layer to QGIS via Python Console):

tablename = "view1"
geometrycol = "geom"

from qgis.core import QgsVectorLayer, QgsDataSourceUri
uri = QgsDataSourceUri()
uri.setConnection("localhost", "5432", "user", "user", "")
uri.setDataSource ("public", tablename, geometrycol)
vlayer=QgsVectorLayer (uri .uri(False), tablename, "postgres")
QgsProject.instance().addMapLayer(vlayer)

The view is created from a source table:

create view view1 as select * from my_table;

Both the source table my_table and the view view1 can be loaded via the QGIS 3 GUI manually without a problem. But when loading the view programmatically using the above code, QGIS crashes on the last line:

QgsProject.instance().addMapLayer(vlayer)

The same code works fine on the source table. And this is with PostgreSQL 11.3, PostGIS 2.5, and QGIS 3.6.

Has anyone else encountered similar problems and know of a solution?

tinlyx
  • 11,057
  • 18
  • 71
  • 119

0 Answers0