How to set some context variable for user / connection

I am currently using Firebird SQL as a backend for my shareware, I would also like to support PG 9.3 +.

In FB, I use set / get_context for this:

set-context

get-context

eg. I would do this after the user logs in:

select rdb$set_context('USER_SESSION', 'LANGUAGE_ID', %s) \
        from rdb$database" % appobj.loggedInUser.language.id

In some of my views, I would use:

... AND t.fk_language_id=rdb$get_context('USER_SESSION', 'LANGUAGE_ID')

I looked through the PG document and did some search queries, but couldn't find a solution.

Would thank for the advice. Werner

+4
source share
1 answer

You can use session variables in a user schema:

postgres=# set myvars.language_id = 10;
SET
postgres=# show myvars.language_id;
 myvars.language_id 
--------------------
 10
(1 row)

or through functions ( http://www.postgresql.org/docs/9.4/static/functions-admin.html ):

postgres=# select set_config('myvars.language_id', '20', false);
 set_config 
------------
 20
(1 row)

postgres=# select current_setting('myvars.language_id');
 current_setting 
-----------------
 20
(1 row)
+8

All Articles