0

I am so new to Django and postgresql. Django made a connection at first step and created my tables, but after that just keep telling 'settings.DATABASES is improperly configured, Please supply the NAME or OPTIONS['service'] value.'. I really don't know what is the issue. I will post some of code related to database. my database is running and I can see my tables there via pgAdmin. Django version is 4.1.4 and psycopg2 version is 2.9.5.

Exception:

Exception Type: ImproperlyConfigured at /accounts/login
Exception Value: settings.DATABASES is improperly configured. Please supply the NAME or OPTIONS['service'] value.

My settings.py


POSTGRES_HOST = os.environ.get('POSTGRES_HOST', default="")
POSTGRES_DB = os.environ.get('POSTGRES_DB', default="")
POSTGRES_USER = os.environ.get('POSTGRES_USER', default="")
POSTGRES_PASSWORD = os.environ.get('POSTGRES_PASSWORD', default="")

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': POSTGRES_DB,
        'USER': POSTGRES_USER,
        'PASSWORD': POSTGRES_PASSWORD,
        'HOST': POSTGRES_HOST,
        'PORT': 5432,
    }
}

My docker-compose.yml

version: "3.11"

x-service-volumes: &service-volumes
  - ./:/app/:rw,cached

x-database-variables: &database-variables
  POSTGRES_DB: postgres
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: postgres

x-app-variables: &app-variables
  <<: *database-variables
  POSTGRES_HOST: postgres

services:
  website:
    image: myproject-website:latest
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes: *service-volumes
    depends_on:
      - db_migrate
    ports:
      - "8000:8000"

  db_migrate:
    image: myproject-website:latest
    command: python3 manage.py migrate
    volumes: *service-volumes
    environment: *app-variables
    depends_on:
      - postgres
  
  postgres:
    image: postgres
    ports:
      - "5432:5432"
    environment: *database-variables
    volumes:
      - db-data:/var/lib/postgresql/data

volumes:
  db-data:

I checked both project and database is running in docker and is listening to new connections. I tried to put real values as default values, like below: POSTGRES_DB = os.environ.get('POSTGRES_DB', default="postgres") and it didn't work either.

Haniyeh Khaksar
  • 774
  • 4
  • 20
  • Not clear to me, does `POSTGRES_DB = os.environ.get('POSTGRES_DB', default="postgres")` work or not?. – Adrian Klaver Dec 18 '22 at 17:20
  • @AdrianKlaver it didn't work. – Haniyeh Khaksar Dec 18 '22 at 17:42
  • 1) Define did not work. 2) I don't work with Docker so I am getting out of my depth here, but I suspect it is an order of execution issue. You might want to look at [Startup order](https://docs.docker.com/compose/startup-order/) as this [Compose](https://docs.docker.com/compose/compose-file/compose-file-v3/) says for it's example: *depends_on does not wait for db and redis to be “ready” before starting web - only until they have been started.* – Adrian Klaver Dec 18 '22 at 18:08
  • about the second part, since website depends on db_migrate, and db_migrate depends on postgres, means website start running after postgres is ready. I added whole exception message I got, it seems it is for specific page, my home page is running just fine, login page is up just fine, but when I click on login button, I got exception. or when I try to run command `make compose-manage-py cmd="createsuperuser"` in my terminal, I get exception. – Haniyeh Khaksar Dec 18 '22 at 18:21
  • 1) That is because until you ask to login(press the login button) you are not connecting to the database you are just starting the Web server and loading pages. The exception is explicit this `'NAME': POSTGRES_DB,` is not being set properly. Try changing to `'NAME': ` where you substitute the database name for **. 2) Read the links I posted, `depends_on` does not necessarily do what you think it does. – Adrian Klaver Dec 18 '22 at 20:02
  • @AdrianKlaver thanks for being with during solving this issue, I just needed to add depends on part to website as well, it didn't get this part from db_migrate, the way I thought it should be. I just add `environment: *app-variables depends_on: - postgres` to website part and all is good now. – Haniyeh Khaksar Dec 19 '22 at 00:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/250517/discussion-between-haniyeh-khaksar-and-adrian-klaver). – Haniyeh Khaksar Dec 19 '22 at 01:01

1 Answers1

0

I needed to change docker-compose.yml in website part like below:

services:
  website:
  image: myproject-website:latest
  command: python3 manage.py runserver 0.0.0.0:8000
  volumes: *service-volumes
  depends_on:
    - db_migrate
    - postgres
  environment: *app-variables
  ports:
    - "8000:8000"
  
Haniyeh Khaksar
  • 774
  • 4
  • 20