Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

344
Visualizações
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)

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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'
}

over 4 years ago · Santiago Trujillo Relatório

0

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

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda