[英]ERROR: Django connect to PostgreSQL via Docker
我的settings.py代码
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'db',
'PORT': 5432
}
}
Dockerfile
# Pull base image
FROM python:3.8
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory
WORKDIR /code
# Install dependencies
COPY Pipfile Pipfile.lock /code/
RUN pip install pipenv && pipenv install --system
# Copy project
COPY . /code/
docker-compose.yml
version: '3.8'
services:
web:
build: .
command: python /code/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- 8000:8000
depends_on:
- db
db:
image: postgres:11
当 docker-compose 登录 cmd 时,我看到一个错误
db_1 | Error: Database is uninitialized and superuser password is not specified.
db_1 | You must specify POSTGRES_PASSWORD to a non-empty value for the
db_1 | superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
db_1 |
db_1 | You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
db_1 | connections without a password. This is *not* recommended.
db_1 |
db_1 | See PostgreSQL documentation about "trust":
db_1 | https://www.postgresql.org/docs/current/auth-trust.html
如果我使用带有 sqlite3 的基本settings.py它可以工作并连接到服务器
所以我是Docker上的新人,怎么解决? 感谢您的回答
这是对我做的
docker-compose.yml(已编辑)
version: '3.8'
services:
web:
build: .
command: python /code/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- 8000:8000
depends_on:
- db
db:
image: postgres:11
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=postgres
volumes:
- postgres_data:/var/lib/postgresql/data/
volumes:
postgres_data:
使用环境变量初始化您的 postgres 容器:
db:
image: postgres:11
environment:
- POSTGRES_USER=example
- POSTGRES_PASSWORD=example
- POSTGRES_DB=example
您必须使用环境变量POSTGRES_PASSWORD
设置PostgreSQL
的密码。
如果要禁用密码要求,可以通过将POSTGRES_PASSWORD: [your_password]
替换为POSTGRES_HOST_AUTH_METHOD: trust
来实现。
job:
build:
docker:
- image: circleci/postgres:9.6
environment:
#...
POSTGRES_HOST_AUTH_METHOD: trust
#...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.