Monday, 12 August 2019

How to change the "application_name" of a PostGIS-connection in QGIS?


PostGIS offers a view called pg_stat_activity, where you can see all the active connections to the database. This view includes the interesting column application_name. When a connection comes from a QGIS instance, the text "QGIS" appears there.


An application can send this parameter to PostGIS in the connection string, and I suppose that QGIS do it so. The connection string looks than more or less like that:


host=... port=... dbname=... user=... password=... application_name=QGIS



I would like to change this value in QGIS to include the username (that I will get from the operating system). Instead of "QGIS", I would like to see something like "QGIS for user1". Is there a was to do that, may be by changing the connection string using a pyQGIS script or any other solution?


Edit:


I just tried following thing: using the python console I made use of the function setEncodedUri which seems allow all possible connection parameters. But it doesn't work: The application_name appears in the layer properties in QGIS but in PostGIS I still just see "QGIS" and nothing more. This are the lines I was using for this test:


    uri = QgsDataSourceURI()
uri.setEncodedUri("host=...&port=...&dbname=...&user=...&password=...&application_name=QGIS for user1")
uri.setDataSource("MySchema", "MyTable", "wkb_geometry", "")
vlayer = QgsVectorLayer(uri.uri(), "MyLayerName", "postgres")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)

Edit2: I had a look in the source code of QGIS 2.8: the trick with setEncodedUri can't work, because the code calls SET application_name='QGIS' just after opening the connection and that overwrites my setting!



I'm still looking for a working solution...


Edit3


It possible to set the environment variable PGOPTIONS to something like "-c application_name=QGISuser1" or even easier the environment varaible PGAPPNAME (see fallback_application_name) before starting QGIS. It's used to change some postgres configuration variables for the current session. I tried it with over variables and it worked. But as QGIS overwrites the value of application_name after opening the connection, it doesn't work for this variable. And I'm now thinking, that there are probably no ways to do what I want with the current version of QGIS.




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...