8

I am trying to open a QGIS project that is saved to the database (Project -> Save to -> PostgreSQL) by means of Python script.

I find a similar way to open project which is saved to the desktop using QgsProject.instance().read(filename).

Can anyone guide me a similar way to open from database?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Madhu
  • 111
  • 5
  • In https://qgis.org/pyqgis/3.0/core/Project/QgsProject.html#qgis.core.QgsProject.read I am not able to find any relevant references to postgresql, so it may look as if it is not directly possible through python, but there are smart people around here, somebody may know something... – MortenSickel Mar 20 '20 at 12:55

1 Answers1

11

You can load a project from a database storage (e.g., PostgreSQL or GeoPackage) in this way:

QgsProject.instance().read(uri)

Where uri is the file name of the project, which you could get from:

QgsProject.instance().fileName()

Since we are dealing with projects stored in databases, the file name is actually a uri.


Sample uris:

  • Project in a PostgreSQL database:

    'postgresql://user:password@localhost:5432?sslmode=disable&dbname=my_db&schema=my_schem&project=my_pg_project'
    
  • Project in a GeoPackage database:

    'geopackage:/path/to/my_db.gpkg?projectName=my_gpgk_project'
    

Related:

Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178