<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:"Trebuchet MS";
        panose-1:2 11 6 3 2 2 2 2 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;
        mso-ligatures:standardcontextual;
        mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
pre
        {mso-style-priority:99;
        mso-style-link:"HTML Vorformatiert Zchn";
        margin:0cm;
        font-size:10.0pt;
        font-family:"Courier New";}
span.HTMLVorformatiertZchn
        {mso-style-name:"HTML Vorformatiert Zchn";
        mso-style-priority:99;
        mso-style-link:"HTML Vorformatiert";
        font-family:Consolas;
        mso-ligatures:standardcontextual;
        mso-fareast-language:EN-US;}
span.im
        {mso-style-name:im;}
span.E-MailFormatvorlage24
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1027" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=DE-CH link=blue vlink=purple style='word-wrap:break-word'><div class=WordSection1><p class=MsoNormal>Versteht jemand wie man das anwendet? Habe Hemmung da Nachzufragen.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Gruss, Peter<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><div><div style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p class=MsoNormal><b><span lang=DE style='mso-ligatures:none;mso-fareast-language:DE-CH'>Von:</span></b><span lang=DE style='mso-ligatures:none;mso-fareast-language:DE-CH'> QGIS-User <qgis-user-bounces@lists.osgeo.org> <b>Im Auftrag von </b>Jorge Gustavo Rocha via QGIS-User<br><b>Gesendet:</b> Dienstag, 8. April 2025 10:54<br><b>An:</b> qgis-user@lists.osgeo.org<br><b>Betreff:</b> Re: [Qgis-user] Storing and Retrieving Vector Layers with Styles to Postgres DB<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p> </o:p></p><p>Hi,<o:p></o:p></p><p>This is the code I use (since some years ago...) to write styles from all layers to the database:<o:p></o:p></p><div><div><p class=MsoNormal style='line-height:12.0pt;background:white'><span style='font-size:9.0pt;font-family:"Courier New";color:black'>mapGeometryType = {</span><span style='font-size:9.0pt;font-family:"Courier New";color:#3B3B3B'><o:p></o:p></span></p></div><div><p class=MsoNormal style='line-height:12.0pt;background:white'><span style='font-size:9.0pt;font-family:"Courier New";color:black'>    </span><span style='font-size:9.0pt;font-family:"Courier New";color:#098658'>0</span><span style='font-size:9.0pt;font-family:"Courier New";color:black'>: </span><span style='font-size:9.0pt;font-family:"Courier New";color:#A31515'>"Point"</span><span style='font-size:9.0pt;font-family:"Courier New";color:black'>,</span><span style='font-size:9.0pt;font-family:"Courier New";color:#3B3B3B'><o:p></o:p></span></p></div><div><p class=MsoNormal style='line-height:12.0pt;background:white'><span style='font-size:9.0pt;font-family:"Courier New";color:black'>    </span><span style='font-size:9.0pt;font-family:"Courier New";color:#098658'>1</span><span style='font-size:9.0pt;font-family:"Courier New";color:black'>: </span><span style='font-size:9.0pt;font-family:"Courier New";color:#A31515'>"Line"</span><span style='font-size:9.0pt;font-family:"Courier New";color:black'>,</span><span style='font-size:9.0pt;font-family:"Courier New";color:#3B3B3B'><o:p></o:p></span></p></div><div><p class=MsoNormal style='line-height:12.0pt;background:white'><span style='font-size:9.0pt;font-family:"Courier New";color:black'>    </span><span style='font-size:9.0pt;font-family:"Courier New";color:#098658'>2</span><span style='font-size:9.0pt;font-family:"Courier New";color:black'>: </span><span style='font-size:9.0pt;font-family:"Courier New";color:#A31515'>"Polygon"</span><span style='font-size:9.0pt;font-family:"Courier New";color:black'>,</span><span style='font-size:9.0pt;font-family:"Courier New";color:#3B3B3B'><o:p></o:p></span></p></div><div><p class=MsoNormal style='line-height:12.0pt;background:white'><span style='font-size:9.0pt;font-family:"Courier New";color:black'>    </span><span style='font-size:9.0pt;font-family:"Courier New";color:#098658'>3</span><span style='font-size:9.0pt;font-family:"Courier New";color:black'>: </span><span style='font-size:9.0pt;font-family:"Courier New";color:#A31515'>"UnknownGeometry"</span><span style='font-size:9.0pt;font-family:"Courier New";color:black'>,</span><span style='font-size:9.0pt;font-family:"Courier New";color:#3B3B3B'><o:p></o:p></span></p></div><div><p class=MsoNormal style='line-height:12.0pt;background:white'><span style='font-size:9.0pt;font-family:"Courier New";color:black'>    </span><span style='font-size:9.0pt;font-family:"Courier New";color:#098658'>4</span><span style='font-size:9.0pt;font-family:"Courier New";color:black'>: </span><span style='font-size:9.0pt;font-family:"Courier New";color:#A31515'>"NullGeometry"</span><span style='font-size:9.0pt;font-family:"Courier New";color:black'>,</span><span style='font-size:9.0pt;font-family:"Courier New";color:#3B3B3B'><o:p></o:p></span></p></div><div><p class=MsoNormal style='line-height:12.0pt;background:white'><span style='font-size:9.0pt;font-family:"Courier New";color:black'>}</span><span style='font-size:9.0pt;font-family:"Courier New";color:#3B3B3B'><o:p></o:p></span></p></div><p class=MsoNormal style='line-height:12.0pt;background:white'><span style='font-size:9.0pt;font-family:"Courier New";color:#3B3B3B'><o:p> </o:p></span></p><div><p class=MsoNormal style='line-height:12.0pt;background:white'><span style='font-size:9.0pt;font-family:"Courier New";color:black'>layers = QgsProject.instance().mapLayers()</span><span style='font-size:9.0pt;font-family:"Courier New";color:#3B3B3B'><o:p></o:p></span></p></div><p class=MsoNormal style='line-height:12.0pt;background:white'><span style='font-size:9.0pt;font-family:"Courier New";color:#3B3B3B'><o:p> </o:p></span></p><div><p class=MsoNormal style='line-height:12.0pt;background:white'><span style='font-size:9.0pt;font-family:"Courier New";color:#AF00DB'>for</span><span style='font-size:9.0pt;font-family:"Courier New";color:black'> layer </span><span style='font-size:9.0pt;font-family:"Courier New";color:#AF00DB'>in</span><span style='font-size:9.0pt;font-family:"Courier New";color:black'> layers.values():</span><span style='font-size:9.0pt;font-family:"Courier New";color:#3B3B3B'><o:p></o:p></span></p></div><div><p class=MsoNormal style='line-height:12.0pt;background:white'><span style='font-size:9.0pt;font-family:"Courier New";color:black'>    </span><span style='font-size:9.0pt;font-family:"Courier New";color:#AF00DB'>if</span><span style='font-size:9.0pt;font-family:"Courier New";color:black'> layer.type() == QgsMapLayer.VectorLayer:</span><span style='font-size:9.0pt;font-family:"Courier New";color:#3B3B3B'><o:p></o:p></span></p></div><div><p class=MsoNormal style='line-height:12.0pt;background:white'><span style='font-size:9.0pt;font-family:"Courier New";color:black'>        </span><span style='font-size:9.0pt;font-family:"Courier New";color:#AF00DB'>if</span><span style='font-size:9.0pt;font-family:"Courier New";color:black'> mapGeometryType[layer.geometryType()] != </span><span style='font-size:9.0pt;font-family:"Courier New";color:#A31515'>"NullGeometry"</span><span style='font-size:9.0pt;font-family:"Courier New";color:black'>:</span><span style='font-size:9.0pt;font-family:"Courier New";color:#3B3B3B'><o:p></o:p></span></p></div><div><p class=MsoNormal style='line-height:12.0pt;background:white'><span style='font-size:9.0pt;font-family:"Courier New";color:black'>            layer.deleteStyleFromDatabase</span><span style='font-size:9.0pt;font-family:"Courier New";color:#3B3B3B'><o:p></o:p></span></p></div><div><p class=MsoNormal style='line-height:12.0pt;background:white'><span style='font-size:9.0pt;font-family:"Courier New";color:black'>            layer.saveStyleToDatabase(</span><span style='font-size:9.0pt;font-family:"Courier New";color:#001080'>name</span><span style='font-size:9.0pt;font-family:"Courier New";color:black'>=layer.name(),</span><span style='font-size:9.0pt;font-family:"Courier New";color:#001080'>description</span><span style='font-size:9.0pt;font-family:"Courier New";color:black'>=</span><span style='font-size:9.0pt;font-family:"Courier New";color:#A31515'>"Default style for </span><span style='font-size:9.0pt;font-family:"Courier New";color:blue'>{}</span><span style='font-size:9.0pt;font-family:"Courier New";color:#A31515'>"</span><span style='font-size:9.0pt;font-family:"Courier New";color:black'>.format(layer.name()), </span><span style='font-size:9.0pt;font-family:"Courier New";color:#001080'>useAsDefault</span><span style='font-size:9.0pt;font-family:"Courier New";color:black'>=</span><span style='font-size:9.0pt;font-family:"Courier New";color:blue'>True</span><span style='font-size:9.0pt;font-family:"Courier New";color:black'>, </span><span style='font-size:9.0pt;font-family:"Courier New";color:#001080'>uiFileContent</span><span style='font-size:9.0pt;font-family:"Courier New";color:black'>=</span><span style='font-size:9.0pt;font-family:"Courier New";color:#A31515'>""</span><span style='font-size:9.0pt;font-family:"Courier New";color:black'>)</span><span style='font-size:9.0pt;font-family:"Courier New";color:#3B3B3B'><o:p></o:p></span></p></div></div><div><p class=MsoNormal>Since we are writing all with the flag "default style" there is no need to load them when opening the project.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>Regards,<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>Jorge Gustavo<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>On 07/04/25 19:11, Ludwig Kniprath via QGIS-User wrote:<o:p></o:p></p></div><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><p>Hello Peter,<o:p></o:p></p><p>everything that users do in QGis can be done by Python scripts, including your steps listed below. <o:p></o:p></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...<o:p></o:p></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...<o:p></o:p></p><p>Just my two cents...<o:p></o:p></p><p>Ludwig<o:p></o:p></p><p><o:p> </o:p></p><div><p class=MsoNormal>Am 07.04.25 um 08:45 schrieb peter.berger--- via QGIS-User:<o:p></o:p></p></div><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><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?</span><o:p></o:p></p><p class=MsoNormal><span lang=FR-CH> </span><o:p></o:p></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</span></b><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'>Brunnwiesenstrasse 62</span><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'>8049 Zürich</span><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'>Schweiz</span><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'>mobile +41 79 301 71 54</span><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'> </span><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'>The several step procedure:</span><o:p></o:p></p><p><span style='font-family:Symbol'>·</span><span lang=FR-CH>  <strong><span style='font-family:"Calibri",sans-serif'>Load the layer</span></strong>: Import the layer from a vector file into QGIS.</span><o:p></o:p></p><p><span style='font-family:Symbol'>·</span><span lang=FR-CH>  <strong><span style='font-family:"Calibri",sans-serif'>Rename (if necessary)</span></strong>: Give the layer an appropriate name.</span><o:p></o:p></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'> </span><o:p></o:p></p><p class=MsoNormal> <o:p></o:p></p><p class=MsoNormal><span style='mso-ligatures:none;mso-fareast-language:DE-CH'><br><br><o:p></o:p></span></p><pre>_______________________________________________<o:p></o:p></pre><pre>QGIS-User mailing list<o:p></o:p></pre><pre><a href="mailto:QGIS-User@lists.osgeo.org">QGIS-User@lists.osgeo.org</a><o:p></o:p></pre><pre>List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user">https://lists.osgeo.org/mailman/listinfo/qgis-user</a><o:p></o:p></pre><pre>Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user">https://lists.osgeo.org/mailman/listinfo/qgis-user</a><o:p></o:p></pre></blockquote><p class=MsoNormal><span style='mso-ligatures:none;mso-fareast-language:DE-CH'><br><br><o:p></o:p></span></p><pre>_______________________________________________<o:p></o:p></pre><pre>QGIS-User mailing list<o:p></o:p></pre><pre><a href="mailto:QGIS-User@lists.osgeo.org">QGIS-User@lists.osgeo.org</a><o:p></o:p></pre><pre>List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user">https://lists.osgeo.org/mailman/listinfo/qgis-user</a><o:p></o:p></pre><pre>Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user">https://lists.osgeo.org/mailman/listinfo/qgis-user</a><o:p></o:p></pre></blockquote><div><p class=MsoNormal><span style='mso-ligatures:none;mso-fareast-language:DE-CH'>-- <o:p></o:p></span></p><div><p class=MsoNormal><span class=im><b><span style='font-size:18.0pt;mso-ligatures:none;mso-fareast-language:DE-CH'>Geomaster, LDA</span></b></span><span class=im><b><span style='mso-ligatures:none;mso-fareast-language:DE-CH'> </span></b></span><span style='mso-ligatures:none;mso-fareast-language:DE-CH'><o:p></o:p></span></p><div><p class=MsoNormal><b><span style='font-size:7.5pt;color:#999999;mso-ligatures:none;mso-fareast-language:DE-CH'>VENHA DESCOBRIR O CAMINHO DO OPEN SOURCE CONNOSC</span></b><b><span style='mso-ligatures:none;mso-fareast-language:DE-CH'> </span></b><span style='mso-ligatures:none;mso-fareast-language:DE-CH'><o:p></o:p></span></p><table class=MsoNormalTable border=0 cellspacing=1 cellpadding=0 width=450 style='width:337.5pt'><tr style='height:25.0pt'><td width=150 valign=top style='width:112.5pt;padding:1.0pt 1.0pt 1.0pt 1.0pt;height:25.0pt'><p class=MsoNormal><span class=im><span style='font-size:7.5pt;mso-ligatures:none;mso-fareast-language:DE-CH'> </span></span><span style='font-size:10.0pt;mso-ligatures:none;mso-fareast-language:DE-CH'><br><span class=im>Rua Afonso Palmeira, 31, 7 D</span><br><span class=im>4715-278 Braga</span><br><span class=im>VAT/NIF 510 906 109</span><br><span class=im>Phone   +351 253 257 173</span><br>Site <a href="http://geomaster.pt" target="_blank">geomaster.pt</a><br>GPS 41.54627, -8.40432 </span><span style='mso-ligatures:none;mso-fareast-language:DE-CH'><br></span><!--[if gte vml 1]><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f">
<v:stroke joinstyle="miter" />
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0" />
<v:f eqn="sum @0 1 0" />
<v:f eqn="sum 0 0 @1" />
<v:f eqn="prod @2 1 2" />
<v:f eqn="prod @3 21600 pixelWidth" />
<v:f eqn="prod @3 21600 pixelHeight" />
<v:f eqn="sum @0 0 1" />
<v:f eqn="prod @6 1 2" />
<v:f eqn="prod @7 21600 pixelWidth" />
<v:f eqn="sum @8 21600 0" />
<v:f eqn="prod @7 21600 pixelHeight" />
<v:f eqn="sum @10 21600 0" />
</v:formulas>
<v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect" />
<o:lock v:ext="edit" aspectratio="t" />
</v:shapetype><v:shape id="Bild_x0020_2" o:spid="_x0000_s1026" type="#_x0000_t75" alt="Logo" style='position:absolute;margin-left:0;margin-top:0;width:37.5pt;height:37.5pt;z-index:251658240;visibility:visible;mso-wrap-style:square;mso-width-percent:0;mso-height-percent:0;mso-wrap-distance-left:0;mso-wrap-distance-top:0;mso-wrap-distance-right:0;mso-wrap-distance-bottom:0;mso-position-horizontal:left;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:line;mso-width-percent:0;mso-height-percent:0;mso-width-relative:page;mso-height-relative:page' o:allowoverlap="f">
<v:imagedata src="cid:part1.he6QvrAY.0HOn8Nsx@geomaster.pt" o:title="Logo" />
<w:wrap type="square" anchory="line"/>
</v:shape><![endif]--><![if !vml]><img width=50 height=50 style='width:.5208in;height:.5208in' src="cid:image002.png@01DBA87E.8B54FCB0" align=left alt=Logo v:shapes="Bild_x0020_2"><![endif]><span style='mso-ligatures:none;mso-fareast-language:DE-CH'><o:p></o:p></span></p></td><td width=25 valign=top style='width:18.75pt;padding:1.0pt 1.0pt 1.0pt 1.0pt;height:25.0pt'><div class=MsoNormal align=right style='text-align:right'><span style='mso-ligatures:none;mso-fareast-language:DE-CH'><hr size=67 width=6 style='width:4.7pt' align=right></span></div></td><td width=150 nowrap valign=top style='width:112.5pt;padding:1.0pt 1.0pt 1.0pt 1.0pt;height:25.0pt'><p class=MsoNormal><span style='font-size:7.5pt;mso-ligatures:none;mso-fareast-language:DE-CH'> </span><span style='font-size:10.0pt;mso-ligatures:none;mso-fareast-language:DE-CH'><br>Jorge Gustavo Rocha <br></span><span style='font-size:7.5pt;color:#999999;mso-ligatures:none;mso-fareast-language:DE-CH'>CTO<br><br></span><span style='font-size:10.0pt;mso-ligatures:none;mso-fareast-language:DE-CH'>Mobile <span class=im>  +351 910 333 888</span><br><span class=im>Email    <a href="mailto:jgr@geomaster.pt">jgr@geomaster.pt</a></span></span><span style='mso-ligatures:none;mso-fareast-language:DE-CH'> <o:p></o:p></span></p></td></tr></table><p class=MsoNormal><span style='mso-ligatures:none;mso-fareast-language:DE-CH'><o:p> </o:p></span></p></div></div></div></div></body></html>