aleph_one
aleph_one

Reputation: 80

Configuring sails app for connection to Heroku Postgres using Environment Variables

Can a sails.js database connection be configured using a URI, or a URL which incorporates the credentials (host, user, password, and database) into one string? Or do they need to be individually assigned as their own key-value pairs in the configuration?

In development, I have configured my sails.js app to use environment variables for database credentials, as such:

in connections.js:

someSqlDatabase: {
    host: process.env.dbHost,
    user: process.env.dbUserName,
    password: process.env.dbPassword,
    database: process.env.db,
    port: 5432,
    ssl: true,
    adapter: 'sails-postgresql'

This works for my connection to a heroku-postgresql resource that I've allocated to my project, and I am able to manually set "Config Vars" in my heroku instance, which I can use in the same way as environment variables in my local instance. By this I mean I can go to the heroku client page for the database resource, copy its credentials (host, database, user, and password) into the heroku app's configuration variables, individually.

The problem is that heroku periodically changes the credentials, and that my manually-copied configuration variables will become outdated, and require re-pasting whenever this happens. Obviously, this is not an ideal solution.

Heroku does automatically provide one configuration variable for the resource, called DATABASE_URL, which looks like:

"postgres://<user>:<password>@<host>:<port>/<database>"

which should be automatically updated, when the credentials change. My problem is that I don't know how to configure a sails.js connection to use this url, in the place of individual "host", "user", "password", and "database" key/value pairs in the the connection config.

I've tried using a configuration that omits the individual keys and uses only "host", as such:

someSqlDatabase: {
   host: process.env.DATABASE_URL,
   ssl: true,
   adapter: 'sails-postgresql'
}

But this connection fails. I've seen this kind of string called a URI, and I've tried the long-shot configuration:

someSqlDatabase: {
    uri: proccess.env.DATABASE_URL
    ssl: true,
    adapter: 'sails-postgresql'
}

but this fails, as well. Is there a way to configure a sails-js connection with heroku postgresql database that will automatically use the latest credentials? (I am using sails v0.12.3)

Upvotes: 1

Views: 803

Answers (2)

Arunabh Das
Arunabh Das

Reputation: 14382

The following is probably the correct way to do this :

module.exports = {
  ...
  default: {
  ssl: true,
  url: process.env.HEROKU_POSTGRESQL_SILVER_URL,
  adapter: 'sails-postgresql'
  },
  ...
}

inside config/env/production.js

Upvotes: 0

Manuel Reil
Manuel Reil

Reputation: 508

I am not using PostgreSQL, but you could try/adapt this:

// before/outside module.exports
var url = require('url').parse(process.env.DATABASE_URL);

// within the config
someSqlDatabase: {
    host: url.host, 
    user: url.auth.split(':')[0], 
    password: url.auth.split(':')[1], 
    database: url.pathname.substring(1),
    port: url.port, 
    ssl: true,
    adapter: 'sails-postgresql'
}

Upvotes: 3

Related Questions