How do I get the username on the current layer PostGIS in QGIS 3.16.2-Hannover? We tried examples: Getting active PostgreSQL connection username when stored through authentication configuration in QGIS 3, but they don't work.
When running this script, variable username
is empty:
layer = iface.activeLayer()
provider = layer.dataProvider()
username = QgsDataSourceUri(provider.dataSourceUri()).username()
print (username)
This is result of provider.dataSourceUri())
:
<QgsDataSourceUri: dbname='postgres' host= localhost port=5432 sslmode=disable key='id' srid=4326 type=Point checkPrimaryKeyUnicity='1' table="locations"."airlines" (geom)>
The value of the username is missing, the question is how to get the username for the current layer?
provider.dataSourceUri()
?