It seems that below code sets the environment value on a system level.
Is there any way to set it with a session level? I mean I need to set the value when I connect to local PostgreSQL database and I don’t want other session to find the value.
there is only one environment variable list per process.
But you can of course change it after connecting or better pass username via connection string.
Does it mean that other processes are not affected with the environment variable My application sets?
yes, you only change them for your app.
For all apps, you need to change Windows registry on Windows or .profile file on Linux or some other file on Mac.