Search code examples
configurationsails.jsenvironment-variablesconfiguration-files

Sails database configuration through environment variables


I used to configure Sails connection to my database in config/env/development.js and config/env/production.js like that:

module.exports = {
   connections: {
      'postgres': {
         host: 'localhost',
         user: 'myUser',
         password: 'myPassword',
         database: 'myDatabase'
      }
   }
};

What if I would like to replace my environment config files by environment variables as explained here?

I expected to use those variables but it doesn't work:

  • sails__connections_postgres_host
  • sails__connections_postgres_user
  • sails__connections_postgres_password
  • sails__connections_postgres_database

Solution

  • Your underscores are backwards. Looking at the docs you linked, your variables should look like:

    sails_connections__postgres__host
    

    etc. (one underscore after 'sails', two underscores in between each key after that).

    Additionally, it is worth noting that you can reference environment variables in your code, so the option exists to refer to an environment variable, 'dbHostname' in your config/env/development.js (or production) as such:

    module.exports = {
        connections: {
            'postgres': {
                host: process.env.dbHostname,
                user: process.env.dbUser,
                password: process.env.dbPassword,
                database: process.env.db
            }
        }
    }
    

    and then create these environment variables upon lifting your server, i.e.

    dbHostname="http://something" dbUser="Your_User" dbPassword="password" db="database" sails lift