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?
1 Answer 1
You can select your PostgreSQL user using the 'PostgreSQL Execute SQL' command in the Processing toolbox.
Query:
SELECT user;
-
how to get user name from script?Andrey Sirbu– Andrey Sirbu2021年08月13日 07:50:01 +00:00Commented Aug 13, 2021 at 7:50
-
How to use python function?Andrey Sirbu– Andrey Sirbu2023年10月18日 05:17:29 +00:00Commented Oct 18, 2023 at 5:17
provider.dataSourceUri()
?