<div dir="ltr">If your local files are exporting but nothing is being pulled from the database I would suspect an authentication/ permissions issue.<br><br>There are a number of ways that QGIS/PG authentication can work in a project, mainly:<br><ul><li>Basic (plaintext) stored in the QGIS Project - Insecure and not recommended, but convenient if you want to make a copy of your project</li><li>Auth manager - Each user profile has it's own qgis-auth.db which is sqlite. You would need to share different auths between users and projects by using the same authid. Note that I don't think that loading the QGIS project from PyQGIS is automatically going to load your auth config though, and it may need to be instantiated in your script [1]. My suspicion is that you are using this method and the QGIS GUI unlocks it for you, resulting in the varied result. You could always try to run most of your script from the python console in the project to check if that works too.</li><li>PGSERVICE - You can also use pg_service.conf (it even supports password=), or similar connection environment variables, which is both convenient and significantly more secure than plaintext credentials in the project. If managed well it can offer a good level of security, but a word of caution is that misuse of it can be a lot more dangerous from a security persepctive. Good for testing purposes though... But note that the way layers are defined and loaded in a project file will vary between service based connections and those managed by the auth manager.</li></ul>You can also make a new (or copy an existing) user profile to define the database connection to prevent the risk of breaking your existing project and db configs.<br><br>I think doing some quick tests to verify whether or not it is an authentication issue is recommended, because auth management can be a bit of a rabbit hole.<br><br>Regards<br><br>[1] <a href="https://docs.qgis.org/3.16/en/docs/pyqgis_developer_cookbook/authentication.html">https://docs.qgis.org/3.16/en/docs/pyqgis_developer_cookbook/authentication.html</a><br>[2] <a href="https://www.postgresql.org/docs/13/libpq-pgservice.html">https://www.postgresql.org/docs/13/libpq-pgservice.html</a><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, 17 Aug 2021 at 23:28, Baker, Matthew <<a href="mailto:MATTHEW_BAKER@dpsk12.org">MATTHEW_BAKER@dpsk12.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div>
<p><font face="Helvetica, Arial, sans-serif">Hi all,</font></p>
<p><font face="Helvetica, Arial, sans-serif">When using PyQGIS with Python 3.8 I can't seem to get the PostGIS layers of a QGIS layout to export to PDF while the local / shapefile layers will.
<br>
</font></p>
<p><font face="Helvetica, Arial, sans-serif">The manual export to PDF from the QGIS Layout window will export the PostGIS layers just fine along with the SHP layers.<br>
</font></p>
<p><font face="Helvetica, Arial, sans-serif">From the code below, can anyone spot what I might be missing?
<br>
</font></p>
<p><font face="Helvetica, Arial, sans-serif">(Running QGIS 3.18 on Linux Mint 20 with Python 3.8)<br>
</font></p>
<p><font face="Helvetica, Arial, sans-serif">Thank you, and let me know if more info is needed!</font></p>
<p><font face="Helvetica, Arial, sans-serif">(BEGIN CODE)<br>
</font></p>
<p><font face="Helvetica, Arial, sans-serif"><font face="Courier
New, Courier, monospace">#!/usr/bin/env python3<br>
import os<br>
from qgis.core import (<br>
#layout and general<br>
QgsProject, QgsLayoutExporter, QgsApplication<br>
)<br>
<br>
QgsApplication.setPrefixPath("/usr", True)<br>
gui_flag = False<br>
app = QgsApplication([], gui_flag)<br>
app.initQgis()<br>
<br>
project_path = os.getcwd() + '/rec_centers.qgz'<br>
project_instance = QgsProject.instance()<br>
project_instance.setFileName(project_path)<br>
project_instance.read()<br>
<br>
#print layer names in layout (not used anywhere else):<br>
names = [<a href="http://layer.name" target="_blank">layer.name</a>() for layer in QgsProject.instance().mapLayers().values()]<br>
print ('Layers in project: ', names)<br>
<br>
#layout stuff<br>
manager = QgsProject.instance().layoutManager()<br>
layout = manager.layoutByName("test_layout") # name of the layout<br>
<br>
#export stuff<br>
exporter = QgsLayoutExporter(layout)<br>
exporter.exportToPdf(project_instance.absolutePath() + "/output/layout.pdf",<br>
QgsLayoutExporter.PdfExportSettings())<br>
<br>
app.exitQgis()</font><br>
</font></p>
<p><font face="Helvetica, Arial, sans-serif">(END CODE)<br>
</font></p>
<p><font face="Helvetica, Arial, sans-serif">-m<br>
</font></p>
<div>-- <br>
<p style="line-height:15.75pt"><span style="font-family:Rockwell,serif;color:rgb(0,118,189)">MATTHEW BAKER<br>
</span><span style="font-size:14px"><span style="font-family:Rockwell,serif;color:rgb(0,118,189)">Spatial Information Manager</span><br>
<span style="font-family:Rockwell,serif;color:rgb(0,118,189)">Denver Public Schools</span><br>
</span><span style="font-size:14px;font-family:Rockwell,serif;color:rgb(0,118,189)">Department of Enrollment and Campus Planning</span></p>
<p style="line-height:15pt"><span style="font-size:12px"><span style="font-family:Tahoma,sans-serif;color:rgb(135,134,135)">1860 Lincoln St., Denver, CO 80203 </span><span style="font-family:Tahoma,sans-serif;color:rgb(0,118,189)">| </span><span style="font-family:Tahoma,sans-serif;color:rgb(135,134,135)">11<sup>th</sup>
Floor</span></span><span style="font-size:12px"><br>
</span><span style="font-family:Tahoma,sans-serif;color:rgb(135,134,135);font-size:12px">720-423-3419</span><span style="font-family:Rockwell,serif;color:rgb(0,118,189)"><span style="font-size:12px"> | matthew_baker@</span><span style="color:rgb(0,118,189);font-size:12px"><a href="http://dpsk12.org" target="_blank">dpsk12.org</a>
| <a href="http://planning.dpsk12.org" target="_blank">planning.dpsk12.org</a></span></span></p>
<p style="line-height:15.75pt"><span style="font-size:13px;font-family:Tahoma,sans-serif;color:rgb(139,191,63)">Students First . </span><span style="font-size:13px"><span style="font-family:Tahoma,sans-serif;color:rgb(2,76,123)">Integrity
. </span><span style="font-family:Tahoma,sans-serif;color:rgb(223,88,42)">Equity . </span><span style="font-family:Tahoma,sans-serif;color:rgb(181,31,110)">Collaboration . </span><span style="font-family:Tahoma,sans-serif;color:rgb(40,165,222)">Accountability
. </span><span style="font-family:Tahoma,sans-serif;color:rgb(173,31,42)">Fun .</span></span></p>
</div>
</div>
_______________________________________________<br>
Qgis-user mailing list<br>
<a href="mailto:Qgis-user@lists.osgeo.org" target="_blank">Qgis-user@lists.osgeo.org</a><br>
List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-user</a><br>
Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-user</a><br>
</blockquote></div>