How can I use an environment variable in a Postgres function? Let's say I have a windows user environment variable SOME_STRING.

Is it possible to use its value in a PL/pgSQL function?

If not, do I need to use an untrusted language (like PL/pythonu) or can I use a trusted one?

I am on PG 9.2
You can not access a environment variable directly; but you can access a custom configuration setting and set that to a value from the environment at startup.  For example, you can pass the follow startup option using PGOPTIONS:

PGOPTIONS="-c 'custom.some_string=${SOME_STRING}'"


Note: the dot (.) in the name is important.

Then retrieve the value using current_setting:

SELECT current_setting('custom.some_string');


(I have tried this on Linux and found it to be working, it should be the same for Windows, but may need some slight modification to cater for that particular platform.)