Search code examples
postgresqlshellenvironment-variablespsqlplpython

PostgreSQL, plpython3u function. os.getenv does not find the environment variable


I get the below situation with PostgreSQL plpython3u function.

I've recently added a environment variable to my shell startup file. The env | grep MY_VAR command returns MY_VAR=/path/. But when I run a select * from my_func(); query from a psql command line, the function says that the environment variable is None.

create or replace function my_func()
returns int as
$$
import os
 for k, v in os.environ.items():
  plpy.info(k + ':' + v)
MY_VAR = os.getenv('MY_VAR')
if MY_VAR is None:
 plpy.info('was not found')
 return -1
plpy.info('MY_VAR:' + MY_VAR)
return 0
$$ language 'plpython3u';
returns int AS

The for k, v in os.environ.items(): ... prints all variables except the one I search for. The MY_VAR gets missed somehow.

Please advice. Why did the function can't find MY_VAR?

Update

The \! env command in psql command prompt also lists MY_VAR


Solution

  • As it turned out, the PostgreSQL server only reads envinronment variables once, upon the main server process start. I had to restart the server to get access to full envinronment varaibles list.