1

I had to backup a PostGIS model in a geopackage and I wonder how to load a non-spatial table, aka a table with NO geometry_column.

1 Answer 1

5

Just had to write None instead of the geometry_column name in the setDataSource method.

uri = QgsDataSourceUri()
uri.setConnection("MyServerAdress", "5432", "myBase", "user", "pass")
uri.setDataSource(schema, table, None)
layer = QgsVectorLayer(uri.uri(False), table, "postgres")
QgsProject.instance().addMapLayer(layer)
1
  • 1
    In my case, it was necessary to add a primary key: uri.setDataSource(schema, table, None, aKeyColumn='id'). Without it, the table was invalid. Mar 28, 2022 at 6:48

Your Answer

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

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