I'm loading layer styles from .qml files in a Python plugin. I'm about to switch to loading them from a non-local PostgreSQL db. I've been looking at the (from QgsVectorLayer (v2.2))

listStylesInDatabase(QStringList &ids, QStringList &names, QStringList &descriptions, QString &msgError)
getStyleFromDatabase(QString styleId, QString &msgError)
loadNamedStyle(const QString &theURI, bool &theResultFlag, bool loadFromLocalDb)

For styles saved as .qml files loading them is straight forward. In terms of Python what are procedure for loading the styles from the db? Are they loaded simultaneously with the loading of the layer? I have so far not found a Q&A on the forum regarding this.

2 Answers 2


(current version: 2.4)

I'm not an expert of the subject and all what I say is based on observation, as reliable as possible.

Styles are loaded automatically when layer are loaded (if you have used the "save in db" option in properties, which link the style to the layer.) With connection to DB, you can have many layers that have the same data source, but with different symbology (eg. ruled based, which is useful). In this case, it will be hard for you to accept the simple automatic load of symbology.

But if you want to do it manually, it's also possible. Currently, it seems that the listStylesInDatabase function has some trouble in pyqgis. It should work as following:

a = []
b = []
c = []
d = ""
for name, layer in layers.iteritems():
     e = layer.listStylesInDatabase(a,b,c,d)
     if e > 0:
          print a,b,c,d

But it's returning juste e, not lists of id, names, etc. So i can propose you another solution, which is not the "official" one, but which can help till the different functions work properly.

You can cannot you to the database usins PyQt4.QtSql (http://pythongisbook.com/blog/2013/04/11/creating-a-postgresql-connection-from-a-qgis-layer-datasource/) And then query the style layer. How ? All style layers are stored in the table layer_styles

SELECT id, f_table_name, description FROM public.layer_styles;

you store the id, or you know. And then apply style for each layer which have at least one in the with following:

errorMsg = []
for name, layer in layers.iteritems():
    get approriate id for layer
    styleText = layer.getStyleFromDatabase(str(id),errorMsg)

I think it should work, after you manage to use QtSql properly :-)


For QGIS 3.X version (3.14 tested)

from qgis.PyQt.QtXml import QDomDocument

layer = iface.mapCanvas().currentLayer()
listedStyles = layer.listStylesInDatabase()
numberOfStyles = listedStyles[0]
defaultStyleId = listedStyles[1][0]
# defaultStyleName = listedStyles[2][0]
# defaultStyleDate = listedStyles[3][0]
if numberOfStyles > 0:
    styledoc = QDomDocument()
    styleTuple = layer.getStyleFromDatabase(defaultStyleId)
    styleqml = styleTuple[0]

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.