<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto">Hi Jorge<div><br></div><div>For me it is too minimal with this code alone. Would it be possible to describe how to run it when I have 65 Layers with styles and like to bring it all with styles to the postgres DB?</div><div><br></div><div>Many thanks in advance.</div><div><br></div><div>Peter</div><div><br></div><div><br id="lineBreakAtBeginningOfSignature"><div dir="ltr">Von meinem iPhone gesendet</div><div dir="ltr"><br><blockquote type="cite">Am 08.04.2025 um 10:59 schrieb Jorge Gustavo Rocha via QGIS-User <qgis-user@lists.osgeo.org>:<br><br></blockquote></div><blockquote type="cite"><div dir="ltr">

  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  
  
    <p>Hi,</p>
    <p>This is the code I use (since some years ago...) to write styles
      from all layers to the database:</p>
    <div style="color: #3b3b3b;background-color: #ffffff;font-family: 'Droid Sans Mono', 'monospace', monospace;font-weight: normal;font-size: 12px;line-height: 16px;white-space: pre;"><div><span style="color: #000000;">mapGeometryType = {</span></div><div><span style="color: #000000;">    </span><span style="color: #098658;">0</span><span style="color: #000000;">: </span><span style="color: #a31515;">"Point"</span><span style="color: #000000;">,</span></div><div><span style="color: #000000;">    </span><span style="color: #098658;">1</span><span style="color: #000000;">: </span><span style="color: #a31515;">"Line"</span><span style="color: #000000;">,</span></div><div><span style="color: #000000;">    </span><span style="color: #098658;">2</span><span style="color: #000000;">: </span><span style="color: #a31515;">"Polygon"</span><span style="color: #000000;">,</span></div><div><span style="color: #000000;">    </span><span style="color: #098658;">3</span><span style="color: #000000;">: </span><span style="color: #a31515;">"UnknownGeometry"</span><span style="color: #000000;">,</span></div><div><span style="color: #000000;">    </span><span style="color: #098658;">4</span><span style="color: #000000;">: </span><span style="color: #a31515;">"NullGeometry"</span><span style="color: #000000;">,</span></div><div><span style="color: #000000;">}</span></div>
<div><span style="color: #000000;">layers = QgsProject.instance().mapLayers()</span></div>
<div><span style="color: #af00db;">for</span><span style="color: #000000;"> layer </span><span style="color: #af00db;">in</span><span style="color: #000000;"> layers.values():</span></div><div><span style="color: #000000;">    </span><span style="color: #af00db;">if</span><span style="color: #000000;"> layer.type() == QgsMapLayer.VectorLayer:</span></div><div><span style="color: #000000;">        </span><span style="color: #af00db;">if</span><span style="color: #000000;"> mapGeometryType[layer.geometryType()] != </span><span style="color: #a31515;">"NullGeometry"</span><span style="color: #000000;">:</span></div><div><span style="color: #000000;">            layer.deleteStyleFromDatabase</span></div><div><span style="color: #000000;">            layer.saveStyleToDatabase(</span><span style="color: #001080;">name</span><span style="color: #000000;">=layer.name(),</span><span style="color: #001080;">description</span><span style="color: #000000;">=</span><span style="color: #a31515;">"Default style for </span><span style="color: #0000ff;">{}</span><span style="color: #a31515;">"</span><span style="color: #000000;">.format(layer.name()), </span><span style="color: #001080;">useAsDefault</span><span style="color: #000000;">=</span><span style="color: #0000ff;">True</span><span style="color: #000000;">, </span><span style="color: #001080;">uiFileContent</span><span style="color: #000000;">=</span><span style="color: #a31515;">""</span><span style="color: #000000;">)</span></div></div>
    <p></p>
    <div class="moz-cite-prefix">Since we are writing all with the flag
      "default style" there is no need to load them when opening the
      project.</div>
    <div class="moz-cite-prefix"><br>
    </div>
    <div class="moz-cite-prefix">Regards,</div>
    <div class="moz-cite-prefix"><br>
    </div>
    <div class="moz-cite-prefix">Jorge Gustavo</div>
    <div class="moz-cite-prefix"><br>
    </div>
    <div class="moz-cite-prefix">On 07/04/25 19:11, Ludwig Kniprath via
      QGIS-User wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:5aaacb54-d7e0-4353-b737-8c98d2c5a1af@kni-online.de">
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <p>Hello Peter,</p>
      <p>everything that users do in QGis can be done by Python scripts,
        including your steps listed below. <br>
      </p>
      <p>There are some difficulties for the desired "one step"
        solution, f. e. the name of the file-based layer must be
        converted in a valid and unique postgis table-name, the fields
        of the file-based layer must be stored with valid postgis
        field-types and -names and so on...</p>
      <p>Taking into account all special features, exceptions and
        necessary settings, this results in a rather complex program,
        the implementation of which may be more complex than the manual
        implementation in QGis...</p>
      <p>Just my two cents...</p>
      <p>Ludwig<br>
      </p>
      <p><br>
      </p>
      <div class="moz-cite-prefix">Am 07.04.25 um 08:45 schrieb
        peter.berger--- via QGIS-User:<br>
      </div>
      <blockquote type="cite" cite="mid:71077542.3128.1744008316438@localhost">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="Generator" content="Microsoft Word 15 (filtered medium)">
        <style>@font-face { font-family: "Cambria Math"; }
@font-face { font-family: Calibri; }
@font-face { font-family: "Trebuchet MS"; }
p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif; }
span.E-MailFormatvorlage17 { font-family: Calibri, sans-serif; color: windowtext; }
.MsoChpDefault { font-family: Calibri, sans-serif; }
div.WordSection1 { page: WordSection1; }</style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
        <div class="WordSection1">
          <p class="MsoNormal">There are serveral steps necessary to
            store from QGIS a vector layer with his style in a postgres
            DB. For a big amout of layers this is impracticaI.<o:p></o:p></p>
          <p class="MsoNormal"><o:p> </o:p></p>
          <p class="MsoNormal">Therfore I would do this in one step from
            QGIS. <span lang="FR-CH">Is this in any way possible?<o:p></o:p></span></p>
          <p class="MsoNormal"><span lang="FR-CH"><o:p> </o:p></span></p>
          <p class="MsoNormal"><b><span style="font-size:8.0pt;font-family:"Trebuchet MS",sans-serif;mso-ligatures:none;mso-fareast-language:DE-CH">Peter
                Berger<o:p></o:p></span></b></p>
          <p class="MsoNormal"><span style="font-size:8.0pt;font-family:"Trebuchet MS",sans-serif;mso-ligatures:none;mso-fareast-language:DE-CH">Brunnwiesenstrasse
              62<o:p></o:p></span></p>
          <p class="MsoNormal"><span style="font-size:8.0pt;font-family:"Trebuchet MS",sans-serif;mso-ligatures:none;mso-fareast-language:DE-CH">8049
              Zürich<o:p></o:p></span></p>
          <p class="MsoNormal"><span style="font-size:8.0pt;font-family:"Trebuchet MS",sans-serif;mso-ligatures:none;mso-fareast-language:DE-CH">Schweiz<o:p></o:p></span></p>
          <p class="MsoNormal"><span style="font-size:8.0pt;font-family:"Trebuchet MS",sans-serif;mso-ligatures:none;mso-fareast-language:DE-CH">mobile
              +41 79 301 71 54<o:p></o:p></span></p>
          <p class="MsoNormal"><span style="font-size:8.0pt;font-family:"Trebuchet MS",sans-serif;mso-ligatures:none;mso-fareast-language:DE-CH"><o:p> </o:p></span></p>
          <p class="MsoNormal"><span style="font-size:8.0pt;font-family:"Trebuchet MS",sans-serif;mso-ligatures:none;mso-fareast-language:DE-CH">The
              several step procedure:<o:p></o:p></span></p>
          <p><span style="font-family:Symbol">·</span><span lang="FR-CH"> 
            </span><strong><span lang="FR-CH" style="font-family:"Calibri",sans-serif">Load
                the layer</span></strong><span lang="FR-CH">: Import the
              layer from a vector file into QGIS.<o:p></o:p></span></p>
          <p><span style="font-family:Symbol">·</span><span lang="FR-CH"> 
            </span><strong><span lang="FR-CH" style="font-family:"Calibri",sans-serif">Rename
                (if necessary)</span></strong><span lang="FR-CH">: Give
              the layer an appropriate name.<o:p></o:p></span></p>
          <p><span style="font-family:Symbol">·</span>  <strong><span style="font-family:"Calibri",sans-serif">Transfer
                the layer to Postgres</span></strong>: Drag the layer
            into the PostgreSQL database. You will find it there with
            the specific name.<o:p></o:p></p>
          <p><span style="font-family:Symbol">·</span>  <strong><span style="font-family:"Calibri",sans-serif">Load
                the layer from Postgres</span></strong>: Import it back
            from the PostgreSQL database into QGIS.<o:p></o:p></p>
          <p><span style="font-family:Symbol">·</span>  <strong><span style="font-family:"Calibri",sans-serif">Assign
                the style</span></strong>: Copy the style from the
            original layer and assign it to the Postgres layer loaded in
            QGIS.<o:p></o:p></p>
          <p><span style="font-family:Symbol">·</span>  <strong><span style="font-family:"Calibri",sans-serif">Export
                the style</span></strong>: Export the style of the
            Postgres layer. You will now find an option to save the
            style with the exact name of the layer in the PostgreSQL
            database.<o:p></o:p></p>
          <p><span style="font-family:Symbol">·</span>  <strong><span style="font-family:"Calibri",sans-serif">Delete
                old layers and reload</span></strong>: Delete all
            associated layers and reload the layer from the PostgreSQL
            database.<o:p></o:p></p>
          <p class="MsoNormal"><span style="font-size:8.0pt;font-family:"Trebuchet MS",sans-serif;mso-ligatures:none;mso-fareast-language:DE-CH"><o:p> </o:p></span></p>
          <p class="MsoNormal"><o:p> </o:p></p>
        </div>
        <br>
        <fieldset class="moz-mime-attachment-header"></fieldset>
        <pre wrap="" class="moz-quote-pre">_______________________________________________
QGIS-User mailing list
<a class="moz-txt-link-abbreviated moz-txt-link-freetext" href="mailto:QGIS-User@lists.osgeo.org" moz-do-not-send="true">QGIS-User@lists.osgeo.org</a>
List info: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-user" moz-do-not-send="true">https://lists.osgeo.org/mailman/listinfo/qgis-user</a>
Unsubscribe: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-user" moz-do-not-send="true">https://lists.osgeo.org/mailman/listinfo/qgis-user</a>
</pre>
      </blockquote>
      <br>
      <fieldset class="moz-mime-attachment-header"></fieldset>
      <pre wrap="" class="moz-quote-pre">_______________________________________________
QGIS-User mailing list
<a class="moz-txt-link-abbreviated" href="mailto:QGIS-User@lists.osgeo.org">QGIS-User@lists.osgeo.org</a>
List info: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-user">https://lists.osgeo.org/mailman/listinfo/qgis-user</a>
Unsubscribe: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-user">https://lists.osgeo.org/mailman/listinfo/qgis-user</a>
</pre>
    </blockquote>
    <div class="moz-signature">-- <br>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <div class="moz-signature"> <span class="im"> <b> <font size="+2">Geomaster, LDA</font> </b> </span><br>
        <div class="moz-signature"> <b> <font color="#999999" size="-2">VENHA DESCOBRIR O CAMINHO DO OPEN SOURCE CONNOSC</font>
          </b>
          <table width="450" border="0" height="126" cellspacing="1" cellpadding="2">
            <tbody>
              <tr>
                <td width="150" height="50" valign="top"> <font size="-1"><span class="im"> <font size="-2"> </font><br>
                      Rua Afonso Palmeira, 31, 7 D<br>
                      4715-278 Braga<br>
                      VAT/NIF </span><span class="im"> 510 906 109<br>
                      Phone</span><span class="im">   +351 253 257 173<br>
                    </span>Site <a href="http://geomaster.pt" rel="noreferrer" target="_blank">geomaster.pt</a><br>
                    GPS 41.54627, -8.40432 </font><br>
                  <span class="im"><div><cropped-geomaster300x300-1.png></div></span>
                </td>
                <td width="25" valign="top" align="right"> <font size="-1"><span class="im"></span></font>
                  <hr width="2" size="100" align="right"> </td>
                <td width="150" height="50" valign="top" nowrap="nowrap">
                  <font size="-1"> <font size="-1"> <font size="-2"> </font><br>
                      Jorge Gustavo Rocha </font><br>
                    <font color="#999999" size="-2">CTO<br>
                      <br>
                    </font>Mobile </font> <font size="-1"><span class="im">  +351 910 333 888<br>
                      Email    <a class="moz-txt-link-abbreviated" href="mailto:jgr@geomaster.pt">jgr@geomaster.pt</a></span></font> </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  
<span>_______________________________________________</span><br><span>QGIS-User mailing list</span><br><span>QGIS-User@lists.osgeo.org</span><br><span>List info: https://lists.osgeo.org/mailman/listinfo/qgis-user</span><br><span>Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user</span><br></div></blockquote></div></body></html>