简体   繁体   English

Postgres django.db.utils.OperationalError:无法连接到服务器:连接被拒绝

[英]Postgres django.db.utils.OperationalError: could not connect to server: Connection refused

Trying to run my django server in a docker, but the postgres port is already being used?试图在 docker 中运行我的 django 服务器,但 postgres 端口已被使用? When I run "docker-compose up", I receive this error:当我运行“docker-compose up”时,我收到此错误:

django.db.utils.OperationalError: could not connect to server: Connection refused
    Is the server running on host "127.0.0.1" and accepting
    TCP/IP connections on port 5432?

ERROR: Service 'web' failed to build: The command '/bin/sh -c python manage.py migrate' returned a non-zero code: 1

sudo service postgresql status须藤服务 postgresql 状态

returns:返回:

9.6/main (port 5432): online

sudo lsof -nP | sudo lsof -nP | grep LISTEN听听

returns:返回:

postgres  15817         postgres    3u     IPv4            1022328        0t0        TCP 127.0.0.1:5432

I tried to run "sudo kill -9 15817", but docker-compose up still receives the same error.我尝试运行“sudo kill -9 15817”,但 docker-compose up 仍然收到相同的错误。

Docker-compose.yml Docker-compose.yml

version: '3'

services:
  db:
    image: postgres
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

settings.py设置.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'stemletics',
        'USER': 'stemleticsadmin',
        'PASSWORD': 'changeme',
        'HOST': '127.0.0.1', # set in docker-compose.yml
        'PORT': 5432 # default postgres port
    }
}

In order to use postgres inside of Docker you will need to configure information like the database user, password and db-name.为了在 Docker 中使用 postgres,您需要配置数据库用户、密码和 db-name 等信息。 This is done through setting environment variables for the container.这是通过为容器设置环境变量来完成的。 A complete list of supported variables can be found here .可在此处找到受支持变量的完整列表。

Additionally you will want to expose port 5432 of postgres to your web service inside your docker-compose file.此外,您需要在docker-compose文件中将 postgres 的端口5432暴露给您的web服务。

Something like this should work:这样的事情应该工作:

docker-compose.yml docker-compose.yml

version: '3'

services:
  db:
    image: postgres
    ports: 
      - "5432"
    environment:
      - POSTGRES_DB=stemletics
      - POSTGRES_USER=stemleticsadmin
      - POSTGRES_PASSWORD=changeme
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

You will also have to change the hostname you are using inside settings.py .您还必须更改在settings.py中使用的主机名。 docker-compose creates a default network for your services and attaches the running containers to this network. docker-compose为您的服务创建一个默认网络,并将正在运行的容器附加到该网络。 Inside your web container the database will be available at the hostname db .在您的 Web 容器内,数据库将在主机名db处可用。

settings.py设置.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'stemletics',
        'USER': 'stemleticsadmin',
        'PASSWORD': 'changeme',
        'HOST': 'db', # set in docker-compose.yml
        'PORT': 5432 # default postgres port
    }
}

Lastly if you do not have any sort of database reconnection logic in your python code the migration may still fail.最后,如果您的 Python 代码中没有任何类型的数据库重新连接逻辑,迁移可能仍会失败。 This is due to the fact that depends_on only waits for the container to start, but postgres will take a couple of seconds to initialze after the container is running.这是因为depends_on只等待容器启动,但是postgres 在容器运行后需要几秒钟的时间来初始化。

In order to get around this quickly it will be easiest to run one container at a time.为了快速解决这个问题,一次运行一个容器是最容易的。

ie: IE:

$ docker-compose up -d db

Wait for postgres to initialize等待 postgres 初始化

$ docker-compose up -d web

Hopefully this gets you up and running.希望这能让你启动并运行。

I was able to fix this issue simply building my db container, wait few seconds, then building the web container:我能够解决这个问题,只需构建我的 db 容器,等待几秒钟,然后构建 web 容器:

docker-compose up -d --build db

wait a few seconds等待几秒钟

docker-compose up -d --build web

I hope this helps我希望这有帮助

I face the same problem to connect the PostgreSQL server to the windows operating system.我在将 PostgreSQL 服务器连接到 Windows 操作系统时遇到了同样的问题。 Then I apply the following way.然后我应用以下方式。 I hope it will help to solve this problem...我希望它有助于解决这个问题......

  1. Download postgres_sql下载postgres_sql
  2. Install postgresql安装 PostgreSQL
  3. Search option open 'SQL Shell (psql)'搜索选项打开“SQL Shell (psql)”
  4. Create database创建数据库

在此处输入图片说明

  1. Settings add DATABASES....设置添加数据库....

settings.py设置.py

 DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'stemletics',
        'USER': 'postgres', #default user name
        'PASSWORD': 'changeme',
        'HOST': '127.0.0.1', # default host
        'PORT': '5432', # default postgres port
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 django.db.utils.OperationalError:无法连接到服务器:连接被拒绝 - django.db.utils.OperationalError: could not connect to server: Connection refused Django 与 Docker 上的 postgreSQL DB - django.db.utils.OperationalError:无法连接到服务器:连接被拒绝 - Django with postgreSQL DB on Docker - django.db.utils.OperationalError: could not connect to server: Connection refused PostgreSQL-django.db.utils.OperationalError:无法连接到服务器:连接被拒绝 - postgresql - django.db.utils.OperationalError: could not connect to server: Connection refused 得到 django.db.utils.OperationalError:无法连接到服务器:连接被拒绝 - Got django.db.utils.OperationalError: could not connect to server: Connection refused kubernetes 上的 Postgres+django:django.db.utils.OperationalError:无法连接到服务器:连接超时 - Postgres+django on kubernetes: django.db.utils.OperationalError: could not connect to server: Connection timed out django.db.utils.OperationalError:无法连接到服务器:没有这样的文件或目录 - django.db.utils.OperationalError: could not connect to server: No such file or directory django.db.utils.OperationalError 无法连接到服务器 - django.db.utils.OperationalError Could not connect to server Docker 和 Django。 django.db.utils.OperationalError: 无法连接到服务器 - Docker and Django. django.db.utils.OperationalError: could not connect to server 在 Dockerfile 中运行“manage.py compilemessages”给出“django.db.utils.OperationalError:无法连接到服务器:没有这样的文件或目录” - Running “manage.py compilemessages” in Dockerfile gives “django.db.utils.OperationalError: could not connect to server: No such file or directory” django.db.utils.OperationalError:无法连接到服务器:Unix 域套接字“/tmp/.s.PGSQL.5432”上没有此类文件? - django.db.utils.OperationalError: could not connect to server: No such file on Unix domain socket “/tmp/.s.PGSQL.5432”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM