6

Context
I am trying to run my Django application and Postgres database in a docker development environment using docker-compose (it's my first time using Docker). I want to use my application with a custom role and database both named teddycrepineau (as opposed to using the default postgres user and db).

Goal
My goal is to deploy a web app powered on the front end by react and the backend by django restapi, the whole running in a docker.

System/Version

  • python: 3.7
  • django: 2.1
  • OS: Mac OS High Sierra

What error am I getting
When running docker-compose up with my custom role and db, I am getting the following error django.db.utils.OperationalError: FATAL: role "teddycrepineau" does not exist. When running the same command with the default role and db postgres Django is able to start normally.

My understanding was that running docker-compose up would create the role and db passed as environment variable.

What I have tried so far
I read multiple threat on this site, GitHub, and docker:

  • tried to delete my container and rebuilt it with formatting as suggested here
  • Went through this GitHub issue
  • Tried to move my environment variable from .env file the environment inside my docker-compose.yml file and rebuild my container

Files

docker-compose.yml

version: '3'

volumes:
  postgres_data: {}

services:
  postgres:
    image: postgres
    volumes: 
      - postgres_data:/var/lib/postgresql/data
    env_file: .env
    ports:
      - "5432"

  django:
    build:
      context: teddycrepineau-backend
      dockerfile: teddycrepineau-root/Dockerfile
    command: ./teddycrepineau-backend/teddycrepineau-root/start.sh
    env_file: .env
    volumes: 
      - .:/teddycrepineau-backend
    ports:
      - "8000:8000"
    depends_on: 
      - postgres

Dockerfile

FROM python:3.7

ENV PYTHONUNBUFFERED 1

WORKDIR /teddycrepineau-backend/
ADD ./teddycrepineau-root/requirements.txt /teddycrepineau-backend/
RUN pip install -r requirements.txt
ADD . /teddycrepineau-backend/
RUN chmod +x ./teddycrepineau-root/start.sh

start.sh

#!/usr/bin/env bash
python3 ./teddycrepineau-backend/teddycrepineau-root/manage.py runserver

.env

POSTGRES_PASSWORD= 
POSTGRES_USER=teddycrepineau
POSTGRES_DB=teddycrepineau

EDIT

My file structure is as follow

root
 |___ teddycrepineau-backend
        |___ teddycrepineau-root
               |___ teddycrepineau
               |___ Dockerfile
               |___ manage.py
               |___ start.sh
 |___ teddycrepineau-frontend
        |___ React-App

 |___ .env
 |___ docker-compose.yml

When I move my docker-compose.yml file inside my backend folder, it starts as expected (though I am not able to access my site when going to 127.0.0.1:8000 but that is mostly a different issue) with custom user and db. When I put my docker-compose.yml file to my root folder, I get the error django.db.utils.OperationalError: FATAL: role "teddycrepineau" does not exist

2
  • 1
    typing error POSTGES_USER=teddycrepineau => POSTGRES_USER=teddycrepineau.
    – Jrog
    Jan 14, 2019 at 3:03
  • 1
    Thanks for pointing this, but I am still getting the same issue.
    – Teddy
    Jan 14, 2019 at 15:10

4 Answers 4

16
+50

This happens because your pgsql db was launched without any envs. The pgsql docker image only uses the envs the first time you created the container, after that it won't recreate DB and users.

The solution is to remove the pgsql volume so next time you docker-compose up you will have a fresh db with envs read. Simple way to do it is docker-compose down -v

8
  • 1
    This is the correct answer. OP says he recreated the container, but the actual tables and data aren't in the container due to the volumes entries. Recreating the container doesn't wipe the existing database and tables, you need to erase everything in "postgres_data". Jan 18, 2019 at 19:42
  • ... does docker-compose down actually remove local files mapped into the container via volumes? Jan 18, 2019 at 19:45
  • 1
    @TimTisdall thanks for reminding me... By default docker-compose down does not remove named volumes, so -v is needed
    – Siyu
    Jan 18, 2019 at 20:09
  • does that actually delete the local files, though? I use volumes to map my code base into an instance and it seems bad if that command actually deleted all contents mapped with a volume. For example, he has '.' mapped to a volume, so wouldn't that command delete the docker-compose.yml?! Jan 18, 2019 at 21:45
  • 1
    Yes that was it. Thank you. I ran docker-compose up the very first time without a .env file but with a named volum. Even though I was running docker-compose down, docker system prune, and docker-compose build it would never delete and rebuild my volum.
    – Teddy
    Jan 22, 2019 at 0:21
3

Change your env order like this.

POSTGRES_DB=teddycrepineau
POSTGRES_USER=teddycrepineau
POSTGRES_PASSWORD= 

I find it at this issue. I hope it works.

1
  • Unfortunately, it did not work. I posted an edit though
    – Teddy
    Jan 15, 2019 at 4:26
0

when you run the

sudo docker-compose exec web python manage.py migrate 

yes of course you will receive

"django.db.utils.OperationalError: FATAL: role "user" does not exist

first you need to put

sudo docker-compose down -v
sudo docker system prune

check container, they should be deleted

sudo docker ps -a

then check images

sudo docker image ls 

don`t forget delete images

sudo docker image rm 3e57319a7a3a 

run to the project folder and then check out

python manage.py migrate 

if it didn`t works put the

python manage.py migrate —run-syncdb

and

sudo docker-compose up -d --build

sudo  docker-compose exec web python manage.py collectstatic --no-input

sudo docker-compose exec web python manage.py makemigrations
sudo docker-compose exec web python manage.py migrate auth
sudo docker-compose exec web python manage.py migrate --run-syncdb
0

I encountered the issue due to a mismatch between the $POSTGRES_DB and $POSTGRES_USER variables. By default, psql will attempt to set the database to the same name as the user logging in, so when there is a mismatch between the variables it fails with an error along the lines of psql:

FATAL: database "root" does not exist

I had to edit the init script that I was writing to include the -d "$POSTGRES_DB" option like so:

#!/bin/bash
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" -d "$POSTGRES_DB" <<-EOSQL
        CREATE USER docker;
        CREATE DATABASE docker;
        GRANT ALL PRIVILEGES ON DATABASE docker TO docker;

EOSQL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.