问题
I've configured docker and postgres for my Django project.
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.
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': '',
}
}
These are the SAME with the ones in the docker-compose.yml
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: In my previous projects, I passed the env file to the docker using env_file instead of using environment section in the docker-compose.yml I did try that too, but its not worked.
I am searching for a long time. Thanks!
回答1:
The hostname in your django config should be the name of the service specified in your docker-compose.yml file. In your case the host should be 'db' and not 'postgres'.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myproject',
'USER': 'admin',
'PASSWORD': 'admin_pass',
'HOST': 'db',
'PORT': '',
}
}
回答2:
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!
Thanks
回答3:
Also spent couple hours for this bug.
For Windows 10 it was the wrong path in the file
\postgres\docker-entrypoint-initdb.d
(this folder is inside your Docker project)
Originally it was
#!/bin/env bash
I made
#!/bin/bash
After this container works fine without problems with local local PostgreSQL.
回答4:
I had the same problem.
And I fixed it that way:
If you are using Docker, and it's listen 5432 port, you can kill the other process that are listen too.
To do this, type this command to see which processes are using port 5432:
$ lsof -i:5432
This will look like this:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
com.docke 15178 andre.carvalho 86u IPv6 0xb128ac87cd34cc69 0t0 TCP *:postgresql (LISTEN)
postgres 16620 andre.carvalho 7u IPv6 0xb128ac87dc50c569 0t0 TCP localhost:postgresql (LISTEN)
postgres 16620 andre.carvalho 8u IPv4 0xb128ac87d2378541 0t0 TCP localhost:postgresql (LISTEN)
After this, it's easy.
Just kill the other process, with the command:
kill -9 16620
Note that the process is identified by the PID.
I hope it helped: D
来源:https://stackoverflow.com/questions/49722565/docker-psql-django-db-utils-operationalerror-fatal-role-admin-does-not-ex