Clint Fidel
Clint Fidel

Reputation: 41

use_env_variable returned undefined in sequelize

I am using the latest version of sequelize and my server/config/config.json file is configured as follows:

{
    "development": {
        "use_env_variable": "DATABASE_URL",
        "dialect": "postgres"
    },
    "test": {
        "use_env_variable": "DATABASE_TEST_URL",
        "dialect": "postgres"
    },
    "production": {
        "use_env_variable": "DATABASE_URL",
        "dialect": "postgres"
    }
}

My .env file is configured as follows:

DATABASE_URL = "MY database URL Here"

When I run sequelize db:migrate I get the following:

Error parsing url: undefined

Upvotes: 4

Views: 10368

Answers (5)

Artiom Oganesyan
Artiom Oganesyan

Reputation: 21

In your .sequelizerc file add require("dotenv").config() somewhere at the top.

Your .env can look like this DATABASE_URL=MY database URL Here.

This should work. If you do not have a .sequelizerc file, check the documentation https://sequelize.org/docs/v6/other-topics/migrations/#the-sequelizerc-file.

Upvotes: 0

Iaroslav Glodov
Iaroslav Glodov

Reputation: 11

I found a solution through the source code: Config file is .js instead of standard .json.

File config/config.js:

module.exports = {
    use_env_variables: "DATABASE_URL",
    dialect: "postgres"
}

And you can run:

DATABASE_URL=postgres://127.0.0.1:5432/mydb npx sequelize-cli db:migrate

But it did not work as expected, new models were added into database with the same name as a username of laptop 🤦‍♂️ I did not have time to find out from sources.

But I found another solution for sequelize-cli where is argument --url is a key:

npx sequelize-cli --url postgres://127.0.0.1:5432/mydb db:migrate

Upvotes: 1

Agent
Agent

Reputation: 1395

Alternative, when you are going to start a server in the terminal remember to config env like this

--env development

--env test

Example to create migration

sequelize db:migrate --env development

sequelize db:migrate --env test

npm run dev --env development

the npm scripts above help me to start server in development environment

Upvotes: 0

Zeeshan Jadoon
Zeeshan Jadoon

Reputation: 1

For windows user

> SET DATABASE_URL=postgresql://[user[:password]@][netlocation][:port][/dbname]

Note: change 'export' to 'SET', working at my side

Upvotes: 0

Yyy
Yyy

Reputation: 79

You aren't defining the variable $DATABASE_URL.

Try this in terminal:

export DATABASE_URL=postgresql://[user[:password]@][netlocation][:port][/dbname]

Upvotes: 7

Related Questions