How to configure environment variable in heroku?


I found myself in need of setting environment variables in heroku for the deployment in production mode of my project.

The variables were the credentials for accessing the data base

const DB_HOST    = process.env.DB_HOST    || 'localhost';
const DB_NAME    = process.env.DB_NAME    || 'nombre_base_dato';
const DB_USER    = process.env.DB_USER    || 'postgres';
const DB_PASS    = process.env.DB_PASS    || 'postgres';
const DB_CHARSET = process.env.DB_CHARSET || 'utf-8';
const DB_CLIENT  = process.env.DB_CLIENT  || 'postgresql';

module.exports = {
  development: {
    client: "postgresql",
    connection: {
        host:     "localhost",
        user:     "postgres",
        password: "postgres",
        database: "abeedb",
        charset:  "utf-8"

production: {
    client: DB_CLIENT,
      connection: {
        host:     DB_HOST,
        user:     DB_USER,
        password: DB_PASS,
        database: DB_NAME,
        charset:  DB_CHARSET
asked by Leonardo Pineda 28.09.2018 в 06:17

1 answer


Heroku allows you from the linux terminal to execute more functions you can visualize it I invite you to execute the following command

heroku help

resulting in this:

 access          manage user access to apps
 addons          manage add-ons
 apps            manage apps
 authorizations  OAuth authorizations
 buildpacks      manage the buildpacks for an app
 certs           a topic for the ssl plugin
 ci              run an application test suite on Heroku
 clients         OAuth clients on the platform
 config          manage app config vars
 domains         manage the domains for an app
 drains          list all log drains
 features        manage optional features
 git             manage local git repository for app
 keys            manage ssh keys
 labs            experimental features
 local           run heroku app locally
 logs            display recent log output
 maintenance     manage maintenance mode for an app
 members         manage organization members
 notifications   display notifications
 orgs            manage organizations
 pg              manage postgresql databases
 pipelines       manage collections of apps in pipelines
 plugins         manage plugins
 ps              manage dynos (dynos, workers)
 redis           manage heroku redis instances
 regions         list available regions
 releases        manage app releases
 run             run a one-off process inside a Heroku dyno
 sessions        OAuth sessions
 spaces          manage heroku private spaces
 status          status of the Heroku platform
 teams           manage teams

To configure the environment variable of our project in heroku, follow the following steps:

  • Start your login from your account and enter your credentials with
    • heroku login
  • You locate yourself on your project or clone yourself if you do not have it
    • cd <Tu Proyecto> or heroku git clone<Tu Proyecto>.git && cd <Tu Proyecto>
  • now ready for configuring the environment variables
    • heroku config:set DB_HOST='escribe-tu-host' heroku config:set DB_USER='escribe-tu-user' heroku config:set DB_PASS='escribe-tu-pass' heroku config:set DB_NAME='escribe-tu-name' heroku config:set DB_CHARSET='escribe-tu-charset'
  • to see the values assigned to the variables only with
    • heroku config:get DB_HOST
  • In addition to this, if you want to see all the variables that you have configured, you can see it with
    • heroku run printenv
  • with heroku run you can execute any command in the heroku hosting and capture the output on your terminal very useful for any need.

    answered by 28.09.2018 в 06:17