6

Is it possible to get login username from a current PostgreSQL layer connection in QGIS?

My goal is to automatically populate new objects in a layer with an editor login username (not @user_account_name or @user_full_name, because they are not related to a postgres connection).

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jul 14, 2018 at 22:08

1 Answer 1

11

I solved the problem by writing a custom expression function:

from qgis.core import *
from qgis.gui import *
@qgsfunction(args='auto', group='Custom')
def get_username(feature, parent, context):
 layer_name = context.variable('layer_name')
 layer = QgsProject.instance().mapLayersByName(layer_name)[0]
 username = QgsDataSourceUri(layer.dataProvider().dataSourceUri()).username()
 return username

now I can use a custom get_username() function in expression from

answered Jul 15, 2018 at 8:47
1
  • 1
    That's great, actually, and there was a question a few weeks ago where I suggested using just this same method, though don't know how to write such functions! Commented Jul 16, 2018 at 15:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.