degrees-of-freedom
degrees-of-freedom

Reputation: 873

Django (createsuperuser) Error: That username is already taken

I am developing a Django application with Docker & PostgreSQL. I was using the same settings I will post below and all was working well, but after I had shutted down and started back (after removing volumes from Docker, and deleting pycache from folder) with:

docker-compose down 
docker-compose up --remove-orphans

When I try to create the admin (same user of the last session) it tells me: Error: That username is already taken. But, when I try to lookup for the user in PgAdmin (localhost:5050) the user does not exists, and when I try to login from the website (localhost:8000) it raise Please enter a correct username and password. Note that both fields may be case-sensitive.

Dockerfile:

FROM python:3.7-slim as production

ENV PYTHONUNBEFFERED=1
WORKDIR /app/

RUN apt-get update && \
    apt-get install -y \
    bash \
    build-essential \
    gcc \
    libffi-dev \
    musl-dev \
    openssl \
    postgresql \
    libpq-dev

COPY requirements/prod.txt ./requirements/prod.txt
RUN pip install -r ./requirements/prod.txt

COPY manage.py ./manage.py
COPY website ./website

EXPOSE 8000

FROM production as development

COPY requirements/dev.txt ./requirements/dev.txt
RUN pip install -r ./requirements/dev.txt

COPY . .

docker-compose.yml

version: "3.7"

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: "django_image:latest"
    command: python manage.py runserver 0.0.0.0:8000
    volumes: *service-volumes
    environment: *app-variables
    depends_on:
      - db_migrate
    ports:
      - "8000:8000"

  db_migrate:
    image: "django_image:latest"
    command: python manage.py migrate
    volumes: *service-volumes
    environment: *app-variables
    depends_on:
      - postgres

  postgres:
    image: postgres:latest
    ports:
      - "5432:5432"
    environment: *database-variables
    volumes:
      - postgres:/data/postgres

  pgadmin:
    container_name: pgadmin_container
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:[email protected]}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
      PGADMIN_CONFIG_SERVER_MODE: "False"
    volumes:
      - pgadmin:/var/lib/pgadmin
    ports:
      - "${PGADMIN_PORT:-5050}:80"
    restart: unless-stopped

volumes:
  postgres:
  pgadmin:

Databases in website/settings.py

DATABASES = {
    'default': {
        'ENGINE': "django.db.backends.postgresql",
        'NAME': "postgres",
        'USER': "postgres",
        'PASSWORD': "postgres",
        'HOST': os.getenv('POSTGRES_HOST'),
        'PORT': 5432
    },
}

It seems to me that the command python manage.py createsuperuser is somehow looking to the past users I had created in the last session and not to what is inside the new postgres db.

Upvotes: 0

Views: 1729

Answers (1)

degrees-of-freedom
degrees-of-freedom

Reputation: 873

Ok, the problem was that I was running generically manage.py createsuperuser. I do not know how, but the command was running and adding on my local postgres db. Therefore, after trying to add something it was already inside the local postgres db. But, of course, both PgAdmin and Django-App were not able to read that. Therefore, the proper command to run was docker-compose run --rm website python manage.py createsuperuser, and it will add properly in the correct postgres db.

Also, my project structure (to which depends the docker-compose command) is:

enter image description here

Hope this will help someone, peace.

Upvotes: 1

Related Questions