Express Sequelize Heroku Postgres Configuration Success

You are in flow developing on your local database and you decide to upload everything to heroku and share it with the world.

How do you configure your production database?

Its easier than you think. Lets assume you have already set up your site. (link)

If you used the sequelize init command, you will have a file db/models/index.js that looks like this

Usually, your sequelize database will initialize with the usual database, username password, etc but in the special case that your config has use_env_variable, sequelize will recognize that an environment variable (usually representing a URI) can be used in place of the usual credentials.

What is your env variable?

After you run heroku addon create ... a new environment variable will be created at your heroku application

Find this in your heroku settings or running $ heroku config

You can access this with process.env.DATABASE_URL in any of your node.js code (on production). Your local machine will have different environment variables set.

Step 1: Set use_env_variable

But set it to what? Set it what heroku designates as your database url, DATABASE_URL!

Thats it!

You may need to migrate your database if you aren’t using sync

Step 2: Migrate your database

Error: no pg_hba.conf entry for host…, SSL OFF

you need to set TWO attributes to specify ssl

Error: The server does not support SSL connections

So tried to migrate your empty production database by setting the correct environment variables for the command and running db:migrate

$ NODE_ENV=production DATABASEURL=<db_url> sequelize db:migrate

How do you set up your local machine to support SSL?? You could get a certificate and configure your sequelize config to use that BUT theres an easier way.

Simply run the command in the heroku environment

$ heroku run ./node_modules/.bin/sequelize db:migrate --debug

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s