问题描述
我已经为Django项目配置了docker和postgres.
I've configured docker and postgres for my Django project.
当我执行 python manage.py runserver 0:8000
时,它给了我这个错误: django.db.utils.OperationalError:致命:角色"admin"不存在
When I do python manage.py runserver 0:8000
,it gives me this error: django.db.utils.OperationalError: FATAL: role "admin" does not exist
但是,我以前的项目使用了相同的配置,在这些项目中一切似乎都很好.
However, I've been used the same configs with my previous projects and everything seems fine in those.
这是我的docker-compose.yml文件
Here is my docker-compose.yml file
version: '3'
services:
db:
image: postgres
environment:
- POSTGRES_DB=myproject
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=admin_pass
container_name: myproject_postgress
volumes:
- ./data/db:/var/lib/postgresql/data
myproject:
build: ./myproject
container_name: myproject
env_file:
- .env
command: "tail -f /dev/null"
volumes:
- ./myproject:/web
ports:
- "8000:8000"
depends_on:
- db
links:
- db:postgres
environment:
- DJANGO_SETTINGS_MODULE=myproject.settings.local
这是我的设置文件中的数据库"部分:
And here is my Database section in my settings file:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myproject',
'USER': 'admin',
'PASSWORD': 'admin_pass',
'HOST': 'postgres',
'PORT': '',
}
}
这些是与docker-compose.yml中的相同的
These are the SAME with the ones in the docker-compose.yml
当我从命令行连接数据库时,可以看到有用户"admin".(当然是本地的)没有码头工人,它不会给出任何错误.那我对docker做错了什么?
When I connect my database from command line, I can see that there is user 'admin'. (locally of course)Without docker, it wont give any error. So what I am doing wrong with the docker?
PS:在我之前的项目中,我使用env_file而不是使用docker-compose.yml中的environment部分将env文件传递给docker.我也尝试过,但是没有用.
PS:In my previous projects, I passed the env file to the docker using env_file instead of using environment section in the docker-compose.ymlI did try that too, but its not worked.
我正在寻找很长一段时间.谢谢!
I am searching for a long time.Thanks!
推荐答案
好一会儿,在帮助下,我弄清楚了.我的系统上有一个 local postgres.我猜,用户"admin"已用于本地postgres.我删除了data/db文件夹并重命名了用户,然后它起作用了!然后,我再次删除了db文件夹,再次将用户重命名为"admin",但是停止了本地postgres,它也起作用了!
Okay, after a while, with a help, I figured it out. There was a local postgres runnning on my system. And the user "admin" has been using for the local postgres I guess.I deleted my data/db folder and rename the user then it worked!Then, I deleted the db folder again, rename the user as "admin" again, but stopped the local postgres, it also worked!
谢谢
这篇关于Docker-psql:django.db.utils.OperationalError:FATAL:角色"admin"不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!