I would like to write a Python macro that upon opening my QGIS project file will reload the default style for each layer from the PostgreSQL 'layer_style' table. I have had a look at the documentation and can only work out how this is done from a SQLite db or .qml file.

Any pointers?

  • Matt, from QGIS 2.0, It will be possible to save layer styles in Postgresql\postgis database. I know it's not the same, but maybe it solves your need. Take a look here https://www.youtube.com/watch?feature=player_embedded&v=zC-_AmmGTQ8 Commented Jun 27, 2013 at 8:53
  • 2
    I have done that, I would like to know if it is possible to reload the default styles from the postgresql layer_style table using Python.
    – Matt
    Commented Jun 27, 2013 at 9:21
  • Sorry, I misunderstood it. Commented Jun 27, 2013 at 9:24

1 Answer 1


Resurrecting the topic with QGIS 3.0 :). Looking at the QgsVectorLayer API documentation instead of QgsMapLayer API, you have the answer. You can use method saveStyleToDatabase

There are related recipes to manipulate styles in PostgreSQL database from QGIS Python tests including saveStyleToDatabase case at https://github.com/qgis/QGIS/blob/master/tests/src/python/test_provider_postgres.py#L1553

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.