I want to get attributes like schema name and table name from PostgreSQL and PostGIS loaded layers in QGIS 3. I've searched in API docs but just found about set data sources, anyone knows how to get this info?

  • you can see the complete DB connection string either when hovering over the layer or in the layer properties' Information tab. does that help? or are you looking for a way to get those values e.g. in pyqgis? – ThingumaBob Feb 14 at 13:19
  • That's right @ThingumaBob I'm looking a way to get this values via pyqgis to do some tasks. I want to do a spatial join with tables which have same name but different schema. – Francisco Camello Feb 14 at 13:34
  • Not sure, but maybe the second part of the answer will help gis.stackexchange.com/questions/246841/… – Cyril Feb 14 at 14:01
  • Unfortunately not @Cyril this answer is for pure SQL. I'm looking for a pyqgis method to do this. Thanks. – Francisco Camello Feb 14 at 15:31
  • I believe that it's simple, because the tool QGIS>properties>details those data are generated... – Cyril Feb 14 at 19:23

You can get this information from your layer with the source() method. It returns the same string that you can see when you hover the layer in the legend, then you can parse it. As the postgres/postgis source contains sensitive data (e.g. password), you can alternatively use publicSource() to get only non-sensitive data.

example code:

layer = iface.activeLayer() #select a postgis layer before running that
source = layer.source()
public_source = layer.publicSource()
print source
print public_source
  • You're welcome! – YoLecomte Feb 27 at 7:39

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.