9

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?

1
  • paste the output of command echo $DATABASE_URL from your shell. Commented Aug 8, 2018 at 11:52

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

This first part is not too helpful as export will make the variables available for all sub-processes, however the user will likely not leave the shell. Sourcing the file with the environmental variables and then running the python script from the same shell should be sufficient. source .myfilename && python myscript.py See this link.
probably i missed source .postgres but in cookiecutter boilerplate they have not used export keyword.
it will only be available within the current shell it is being run in @tahesse
1

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.

Comments

1
POSTGRES_HOST=postgres
POSTGRES_PORT=5432
POSTGRES_DB=database
POSTGRES_USER=postgres
POSTGRES_PASSWORD=password

and them use the variables on the DATABASE_URL, so if you change any POSTGRES_* will affect the DATABASE_URL

DATABASE_URL=postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:$POSTGRES_PORT/$POSTGRES_DB

1 Comment

Why is this not set when Cookicutter django sets up the project?
0

For a python based solution check this link (credits to user @falsetru).

Basically, add

import os

with open('.envs/.local/.postgres') as fh:
    os.environ.update(line.strip().split('=', 1) for line in fh)

in one of your initial python scripts, e.g. local.py.

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.