4

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?

10
  • 1
    What does "they don't work" mean?
    – Ian Turton
    Jan 9 at 18:55
  • 1
    I have tried it as well. I got also username is empty but the reason (in my case) I am connected with local credentials so there is no user and psw. Can you check if you see the 'user' when you print out provider.dataSourceUri()?
    – Nil
    Jan 10 at 13:39
  • I don't understand: you are referring to a question that gives you the solution, doesn't it ?
    – dd_a
    Jan 10 at 16:33
  • the solution to the question I am referring to does not answer my question. Function - QgsDataSourceUri(provider.dataSourceUri()).username() return empty string Jan 11 at 8:05
  • 1
    @AndreySirbu what does it return if you call only provider.dataSourceUri()??
    – Nil
    Jan 11 at 8:11
0

You can select your PostgreSQL user using the 'PostgreSQL Execute SQL' command in the Processing toolbox.

Query:

SELECT user;

enter image description here

1
  • how to get user name from script? Aug 13 at 7:50

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.