7

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?

  • In qgis.org/pyqgis/3.0/core/Project/… 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 at 12:55
7

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: Save QGIS project to database using PyQGIS

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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