I would like to duplicate an entry in QGIS (2.18.21) layer_styles table (in a PostGIS database) by the following steps:

From a PostGIS connection with styles saved to database add layer_styles table to the project and select it in the layers panel.

Using the following code on the python console I try to duplicate one of the features in the table:

>>> l = iface.activeLayer()
>>> f = [f for f in l.getFeatures()]
>>> f
[<qgis._core.QgsFeature object at 0x0000000029F4C378>, <qgis._core.QgsFeature object at 0x0000000029F4A048>]
>>> l.addFeature(f[0])

Now why does addFeature() return False here and how can I duplicate an entry in layer_styles table with PyQGIS?

  • with duplicate you mean take the style, which is associated with layer A and apply it to layer B?
    – LaughU
    Nov 14, 2018 at 15:17
  • that would be the second step, sth like f = f[0], f.setAttribute('layername', 'layer B') and then l.addFeature(f), but I fear this would fail even more so... Nov 14, 2018 at 15:25

1 Answer 1


if you want to take a style which is stored in the PostGIS Table layer_styles you don't apply it to the features but to the whole layer.

I Propose this solution which does not need a duplicated style:

layer = iface.activeLayer() # Holds table B
sql= "select  styleqml from public.layer_styles WHERE stylename = '%s'" %( "Your table name A")
query = QSqlQuery(self.db) # holds connection to your db
while query.next():
    style  = query.record().value("styleqml")

With this approach, you get the style from layer A to layer B

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.