1

I'm using sequelize migrations. Here is my config.js:

require('dotenv').config()

module.exports = {
  development: {
    database: process.env.DEV_DATABASE,
    host: process.env.DEV_HOST,
    dialect: 'postgres'
  },
  test: {
    database: process.env.TEST_DATABASE,
    host: process.env.TEST_HOST,
    dialect: 'postgres'
  },
  production: {
    database: process.env.DATABASE_URL,
    dialect: 'postgres'
  },
}

When I deploy to heroku and run:

heroku run npx sequelize-cli db:migrate

I get the error:

Loaded configuration file "config/config.js".
Using environment "production".

ERROR: connect ECONNREFUSED 127.0.0.1:5432

I have tried changing my config to use_env_variable: DATABASE_URL and I get Error reading "config/config.js". Error: ReferenceError: DATABASE_URL is not defined

If I console.log(process.env.DATABASE_URL) I get something like:

postgres://bczwqcbnwftivi:80bbc89546c0953e0ffd221a950cac249d25d4ef1812127054af7c2f504b2c39@ec2-54-225-205-79.compute-1.amazonaws.com:5432/d8gsvl0n8qilcs

2 Answers 2

5

This worked:

{
    "development": {
        "database": "products_api_development",
        "dialect": "postgres"
    },
    "test": {
        "database": "products_api_test",
        "dialect": "postgres"
    },
    "production": {
        "use_env_variable": "DATABASE_URL",
        "dialect": "postgres",
        "dialectOptions": {
            "ssl": true
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

yeah it is correct if we want to use database url in production we need to add the key of env variable into here "use_env_variable": "DATABASE_URL", note DATABASE_URL key is the name you save database name within .evn file
-1

Here's further documentation for configuring the sequelize config file.

More info on setting up environment variables on Heroku:

You can set up environment variables at https://dashboard.heroku.com/apps/[your-app-name]/settings. In the case of node.js those variables will then be available in your app using process.env.[variable-name].

Settings

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.