I want to add new layer from PostGIS database but this code isn't working... Can someone help me?

uri = QgsDataSourceURI()
uri.setConnection("IP address", "5432", "ruian", "user", "pass", QgsDataSourceURI.SSLdisable)
uri.setDataSource("public", "okresy.originalnihranice", "MultiPolygon", "", "ogc_fid")
layer = QgsVectorLayer(uri.uri(), "okresy", "postgres")

if not layer.isValid():
    print "Layer %s did not load" % layer.name()


layer is always empty


I would suggest looking into this post Empty layer after import into PostGIS Database. Pyqgis

The user mentioned to

make sure that you are looking at using psycopg2 python module. This should have been installed when you got PostgreSQL and PostGIS in your system.

created my new user with privileges of superuser for being able to add postgis extension in new database

use pgadmin3 to create a new connection and to add postgis extension to this database.

| improve this answer | |
  • Thank you for your reaction. I just solved my probem. I had to split name of table "okresy" and "originalnihranice". – Miroslav Starý Oct 10 '17 at 15:53

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