django test app error - Got an error creating the test database: permission denied to create database

后端 未结 9 948
甜味超标
甜味超标 2020-12-22 16:53

When I try to test any app with command (I noticed it when I tried to deploy myproject using fabric, which uses this command):

python manage.py test appname
         


        
9条回答
  •  半阙折子戏
    2020-12-22 17:21

    Wow so combining all of the answers here with a little tweaking finally got me to a working solution for docker-compose, django, and postgres...

    First the postgres command given by noufal valapra is not correct (or maybe just not current), it should be:

    ALTER USER docker WITH CREATEDB;
    

    In the case of a docker-compose setup, this will go in the init.sql file, this is what mine looks like:

    CREATE USER docker;
    ALTER USER docker WITH CREATEDB;
    CREATE DATABASE djangodb;
    GRANT ALL PRIVILEGES ON DATABASE djangodb TO docker;
    

    Then the Dockerfile for postgres looks like this:

    FROM postgres:10.1-alpine
    COPY init.sql /docker-entrypoint-initdb.d/
    

    Then the Django settings.py has this entry:

    if 'RDS_DB_NAME' in os.environ:
        INTERNAL_DATABASES = {
            'default': {
                'ENGINE': 'django.db.backends.postgresql_psycopg2',
                'NAME': os.environ['RDS_DB_NAME'],
                'USER': os.environ['RDS_USERNAME'],
                'PASSWORD': os.environ['RDS_PASSWORD'],
                'HOST': os.environ['RDS_HOSTNAME'],
                'PORT': os.environ['RDS_PORT'],
            }
        }
    

    and the docker-compose looks like this:

    version: '3.6'

    services:

    postgresdb:
      build:
        context: ./
        dockerfile: ./Dockerfile-postgresdb
      volumes:
        - postgresdata:/var/lib/postgresql/data/
    
    django:
      build:
        context: ../
        dockerfile: ./docker/Dockerfile
      environment:
        - RDS_DB_NAME=djangodb
        - RDS_USERNAME=docker
        - RDS_PASSWORD=docker
        - RDS_HOSTNAME=postgresdb
        - RDS_PORT=5432
    
      stdin_open: true
      tty: true
      depends_on:
        - postgresdb
    
    volumes:
        postgresdata:
    

提交回复
热议问题