Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the DATABASE_URL environment variable

I am trying to create my own kind of project structure by looking at two scoops of django. The project structure looks something like this

.envs

.local

  .django

  .postgres

.production

 .django

 .postgres

in postgres inside local, the following items are listed

POSTGRES_HOST=postgres
POSTGRES_PORT=5432
POSTGRES_DB=marketing
POSTGRES_USER=username
POSTGRES_PASSWORD=password
DATABASE_URL=postgres://username:password@localhost:5432/marketing

the settings file is divided into 3 parts as base.py, local.py and production.py

in base.py, I have configured the DATABASES key as following

DATABASES = {
    'default': env.db('DATABASE_URL'),
}

However I am getting an error

django.core.exceptions.ImproperlyConfigured: Set the DATABASE_URL environment variable

Though, I have DATABASE_URL in .envs/.local/.postgres, I am getting above error. Why is that so?

like image 889
Serenity Avatar asked Aug 08 '18 11:08

Serenity


2 Answers

Add export before your variables in .myfilename.

export POSTGRES_HOST=postgres
export POSTGRES_PORT=5432
export POSTGRES_DB=marketing
export POSTGRES_USER=username
export POSTGRES_PASSWORD=password
export DATABASE_URL=postgres://username:password@localhost:5432/marketing

Then do

source .myfilename

like image 166
tushortz Avatar answered Oct 18 '22 04:10

tushortz


I came across same issue recently. Your DATABSE_URL env variable in file .local/.postgres needs to be : DATABASE_URL=postgres://username:password@{DB_SERVICE_NAME}:5432/marketing

As you can see here in place of localhost use "postgres" if your docker-compose.yml has service name as postgres.

like image 1
Dishant Chavda Avatar answered Oct 18 '22 04:10

Dishant Chavda