Skip to content

Instantly share code, notes, and snippets.

@mogul
Last active February 16, 2019 09:01
Show Gist options
  • Save mogul/b266c7cf37f88ee27ae31580da511bf4 to your computer and use it in GitHub Desktop.
Save mogul/b266c7cf37f88ee27ae31580da511bf4 to your computer and use it in GitHub Desktop.
bash-3.2$ DATABASE_URL='postgres://username:password@hostname.net:port/dbname'
bash-3.2$ [[ $DATABASE_URL =~ (.*)'://'(.*):(.*)@(.*):(.*)/(.*) ]] && \
DB_USERNAME=${BASH_REMATCH[2]} \
DB_PASSWORD=${BASH_REMATCH[3]} \
DB_HOST=${BASH_REMATCH[4]} \
DB_PORT=${BASH_REMATCH[5]} \
DB_DBNAME=${BASH_REMATCH[6]} env |grep DB_
DB_PORT=port
DB_USERNAME=username
DB_DBNAME=dbname
DB_PASSWORD=password
DB_HOST=hostname.net
@mogul
Copy link
Author

mogul commented Feb 16, 2019

Regex for extracting individual elements of a postgres URI and assigning them to environment variables so they can be passed into a program individually

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment