将 Django docker 容器连接到 Postgres 数据库时,我收到以下错误。
File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line
130, in connect conn = _connect(dsn,
connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server:
Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
下面是我运行容器的 Dockerfile
FROM python:3.6
MAINTAINER c15523957
RUN apt-get -y update
RUN apt-get -y upgrade
RUN apt-get -y install libgdal-dev
RUN mkdir -p /usr/src/app
COPY requirements.txt /usr/src/app/
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
这是我的 Django 代码的 settings.py 文件中的代码
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'place_loc_db',
'USER': 'place_loc_user',
'PASSWORD': 'abc123',
'HOST': 'localhost',
'PORT': 5432,
}
}
注意:我没有将 postgres 数据库作为容器运行,而是在本地计算机上运行
注意:以下详细信息包含在我的 pg_hba.conf 和 postgresql.conf 文件中
postgresql.conf- >listen_addresses = '*'
pg_hba.conf -> host all all 0.0.0.0/0 md5
我已经阅读了上面打开数据库连接的以下详细信息。