Skip to content

Instantly share code, notes, and snippets.

@mikermcneil
Last active February 19, 2016 07:23
Show Gist options
  • Save mikermcneil/7668424 to your computer and use it in GitHub Desktop.
Save mikermcneil/7668424 to your computer and use it in GitHub Desktop.
How to configure a Sails.js app with a remote Postgres database hosted on Heroku

Using a PostgreSQL database on Heroku with Sails.js

The trick to setting up a Sails.js app with a remote Postgres database hosted on Heroku is all about SSL. Here's how you do it:

In your adapters.js file

Note: This is for Sails v0.9.x, the stable release at the time of this writing.

In Sails v0.10.x, the config/adapters.js file has been replaced with config/connections.js You'll also want to replace the module key with adapter.

It should work anyways, since backwards compatibility for the old config is built-in, but that compatibility will be dropped prior to v1.0, so you might as well set it up to use the latest and greatest.

// ....
'db-core': {
	module: 'sails-postgresql',
	host: process.env.PG_HOSTNAME' || localhost',
	user: process.env.PG_USER || 'root',
	password: process.env.PG_PASSWORD || '',
	database: process.env.PG_DATABASE || 'name_of_your_db',
	port: process.env.PG_PORT || 5432,
	ssl: {
	  rejectUnauthorized: false
	}
}
// ....
@smrutimandal
Copy link

Hi,

This post may need to be updated. The current heroku config gives a different URL.

e.g. DATABASE_URL: postgres://vhsmxoldrngunv:G5Dhk-OEXaiBy6NRucg0DbuEQW@ec2-54-83-198-111.compute-1.amazonaws.com:5432/d3qir1r07g6hi7

Could you please suggest how to connect to this database using connections.js file.

Sample connections file.

postgresql: {
      adapter: 'sails-postgresql',
      database: 'datarepo',
      host: 'localhost',
      user: 'dbuser',
      password: 'dbuserpass',
      port: 5432,
      pool: false,
      ssl: false
    },

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