Skip to content

Instantly share code, notes, and snippets.

@liquidgenius
Forked from davidradernj/py_environ.sql
Created August 8, 2020 14:43
Show Gist options
  • Save liquidgenius/3648ff606ac9cfde05895d62fb83a860 to your computer and use it in GitHub Desktop.
Save liquidgenius/3648ff606ac9cfde05895d62fb83a860 to your computer and use it in GitHub Desktop.
plpython to read environment variable from PostgreSQL
create extension plpythonu;
create type py_environ_type as (name text, value text);
create or replace function py_environ(name varchar DEFAULT NULL)
returns setof py_environ_type
as $$
import os
aev = []
if name is None:
for k, v in os.environ.items():
aev.append((k, v))
else:
v = os.getenv(name)
if v is not None:
aev.append((name,v))
return aev;
$$ language plpythonu
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment