dennisaleynikov
dennisaleynikov

Reputation: 69

How to make heroku play nice with sequelize.js + postgres?

I'm most of the way there, has anyone gotten it to succesfully work? I only have the database working locally, after I git push heroku master the connection to the database fails.

Upvotes: 2

Views: 2460

Answers (1)

Ferdinand Cruz
Ferdinand Cruz

Reputation: 76

  1. Add 'Heroku Postgres' either through the Heroku CLI or under your project's Resources tab. It should also give you a DATABASE_URL variable which you need for later.

  2. Add pg module from Heroku. See the documentation here.

    $ npm install --save --save-exact pg
    

    Then, on the server, use the DATABASE_URL we just got.

    const client = new Client({
      connectionString: process.env.DATABASE_URL,
      ssl: true,
    });
    
    client.connect();
    
  3. Put "use_env_variable": "DATABASE_URL" on the config.json file under "production". Should look like

    "production": {
        "use_env_variable": "DATABASE_URL"
        ...
    }
    
  4. Run migrations on Heroku. An option if you want to use sequelize-cli is:

    $ npm install sequelize-cli --save
    $ heroku run sequelize db:migrate
    

And it should work.

Upvotes: 6

Related Questions