Skip to content

Instantly share code, notes, and snippets.

@midwire
Last active February 16, 2021 18:01
Show Gist options
  • Save midwire/5952fc33736971b093ef72f490728a62 to your computer and use it in GitHub Desktop.
Save midwire/5952fc33736971b093ef72f490728a62 to your computer and use it in GitHub Desktop.
[Access Environment Variables With Postgres] Use environment variables with postgresql #postgres
-- Before starting postgres server
-- export ENVNAME='whatever'
-- pg_ctl -D /usr/local/var/postgres [etc...] start
DROP TABLE IF EXISTS env;
DROP TABLE IF EXISTS env_tmp;
CREATE TEMP TABLE env_tmp(e text);
CREATE TEMP TABLE env(k text, v text);
COPY env_tmp ( e ) FROM PROGRAM 'env';
INSERT INTO env
SELECT (regexp_split_to_array(e,'={1,1}'))[1],
(regexp_match(e, '={1,1}(.*)'))[1]
FROM env_tmp;
SELECT * FROM env WHERE k = 'ENVNAME';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment