[Qgis-user] How load Postgis layer from command line?

Richard Duivenvoorde rdmailings at duif.net
Tue Dec 9 03:02:04 PST 2014


Hi Germán,

it took some time for me too to make it work :-)

This one was easy, and working immediately:

uri = QgsDataSourceURI()
#                  host,       port,   dbase, user, password
uri.setConnection("localhost", "5432", "bag", "geo", "xxx")
#                 schema,      table,     geometry,  whereclause
uri.setDataSource("bag8mrt2014", "provincie", "geovlak", "")
#                                  layername, providername
vlayer = QgsVectorLayer(uri.uri(), "test", "postgres")
# just checking, should return True
vlayer.isValid()
# add it to mapcanvas
QgsMapLayerRegistry.instance().addMapLayer(layer)

but the one you want to use, with a uri string, took me some time to
figure out. Best is to run QGIS in debug mode in terminal, to see if it
is a query problem ( which you get when you do sql=''), or other problems.

But for the above postgis layer, this is working as an uri example:

l = iface.addVectorLayer('dbname=bag host=localhost port=5432 user=geo
password=xxx sslmode=disable key=gid estimatedmetadata=true srid=28992
type=MULTIPOLYGON table="bag8mrt2014"."provincie" (geovlak) sql=',
"test", "postgres")

'problems' you face when you use this:
- do NOT use quotes, EXCEPT for the schema/table value
- the table value should be double quotes, so the full string is single
- do NOT forget the (geovlak) <- geometry! part as it returns a valid
layer then, but you do not see anything.

above uri string is actually taken from the debug output of QGIS when
you load this layer via QGIS itself, and then stripped from quotes.

hope this helps!

Regards,

Richard Duivenvoorde




On 09-12-14 00:59, Germán Carrillo wrote:
> Hi all, 
> 
> could you please let me know of a working command that allows one to
> load a PostGIS layer into QGIS at startup?
> 
> I've tried with the information provided in the PyQGIS Cookbook [1] with
> no success. 
> 
> Specifically, I tried with the uri.uri() string, something
> like: "dbname='buscador' host=localhost port=4326 user='postgres'
> password='postgres' table='public'.'parques_nacionales' (geom) sql="
> But get a "Layer is not valid error."
> 
> I'm using QGIS v.2.6 on GNU/Linux.
> 
> Regards, 
> 
> Germán 
> --
> [1]
> http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/loadlayer.html
> 
> 2013-12-05 8:19 GMT-05:00 Richard Duivenvoorde <rdmailings at duif.net
> <mailto:rdmailings at duif.net>>:
> 
>     On 05-12-13 11:33, Uggla Henrik wrote:
>     > Is there no documentation about this? The extension mentioned is Postgis itself, not a specific Qgis extension, right?
> 
>     Off course there is documentation, did you not read it first ;-)
> 
>     http://www.qgis.org/en/docs/pyqgis_developer_cookbook/loadlayer.html#vector-layers
> 
>     Regards,
> 
>     Richard Duivenvoorde
> 
>     _______________________________________________
>     Qgis-user mailing list
>     Qgis-user at lists.osgeo.org <mailto:Qgis-user at lists.osgeo.org>
>     http://lists.osgeo.org/mailman/listinfo/qgis-user
> 
> 
> 
> 
> -- 
> -----------
>    |\__ 
> (:>__)(
>    |/   
> 
> Soluciones Geoinformáticas Libres                           
> http://geotux.tuxfamily.org/
> http://twitter.com/GeoTux2




More information about the Qgis-user mailing list