Re: passing linux user to PG server as a variable ? - Mailing list pgsql-general

From David Gauthier
Subject Re: passing linux user to PG server as a variable ?
Date
Msg-id CAMBRECDFxVA1rov-bC8_7aPQGF-T4zCZrw+16+FLC0NUjt-KJg@mail.gmail.com
Whole thread Raw
In response to Re: passing linux user to PG server as a variable ?  ("David G. Johnston" <david.g.johnston@gmail.com>)
Responses Re: passing linux user to PG server as a variable ?  ("David G. Johnston" <david.g.johnston@gmail.com>)
Re: passing linux user to PG server as a variable ?  ("Daniel Verite" <daniel@manitou-mail.org>)
List pgsql-general
OK, trying to piece together something that might work but I don't see the pieces falling into place.  
From the link you provided...

"The most fundamental way to set these parameters is to edit the file postgresql.conf"
So I'm fine with asking our IT guys to stick some lines in there for us. But will the thing that executes the file understand what $USER is ?  Will this work...   "osuser = $USER"

I tried this sort of thing through $PGOPTIONS...
setenv PGOPTIONS "-c 'osuser=$USER'"
But when I go to connect...
psql: FATAL:  unrecognized configuration parameter "'osuser"

I can avoid the error by just throwing a namespace in there...
atletx7-reg036:/home/dgauthie[ 214 ] --> setenv PGOPTIONS "-c 'os.user=$USER' "
But once in, "show os.user" is undefined.

I'm fine with a temp table approach, but don't really know where/how to create it in terms of pg sys files, init scripts or env vars like PGOPTIONS. 




On Mon, Aug 17, 2020 at 4:07 PM David G. Johnston <david.g.johnston@gmail.com> wrote:
On Mon, Aug 17, 2020 at 12:53 PM David Gauthier <davegauthierpg@gmail.com> wrote:
Looking at psql command line options, I see "-v" (lowercase) which is described as...

-v assignment
--set=assignment
--variable=assignment

Perform a variable assignment, like the \set meta-command. Note that you must separate name and value, if any, by an equal sign on the command line. To unset a variable, leave off the equal sign. To set a variable with an empty value, use the equal sign but leave off the value. These assignments are done during a very early stage of start-up, so variables reserved for internal purposes might get overwritten later.

So I tried that without success.  
"-v sysinfo.osuser=foo" 
failed the connect with...
"psql: could not set variable "sysinfo.osuser""

Next I tried...
"-v osuser=foo"
This didn't fail the connect, but once I got in...
"show osuser" gave... 
"ERROR:  unrecognized configuration parameter "osuser""
The part of the description that says "like the \set meta-command" means you need to read its description as well.  There, and through links therein, you will learn that what you are creating is a variable within the psql client itself, not on the server.  Usage of that client-side variable is documented.  As long as you don't need the variable on the server, and oftentimes you do not, then this feature will work just fine for you.

The SHOW SQL command (and other documented options[1]) is a server command and inspects server variables.  If you really need to create one of those in the current session it may be possible - though I believe you have to use a namespace prefix (i.e., your sysinfo.osuser) to get the system to recognize a user-created variable name.  There is some work on improving things in this area.  Though worse case you can just stick the desired value into a temporary table and maybe create some function wrappers to modify/access it.

David J.




pgsql-general by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: passing linux user to PG server as a variable ?
Next
From: "David G. Johnston"
Date:
Subject: Re: passing linux user to PG server as a variable ?