简体   繁体   English

无法将主机名“db”转换为地址:名称解析暂时失败

[英]could not translate host name "db" to address: Temporary failure in name resolution

I am trying to build postgres with django from official docker documentation samples link nevertheless for unknown reason i am getting following error which i tried to solve without success.我正在尝试使用来自官方 docker 文档示例链接django构建 postgres,但是由于未知原因,我遇到了以下错误,我试图解决但没有成功。 Please of help.请帮忙。

This is output with error i got:这是 output 我得到的错误:

╰─ docker-compose up
[+] Running 2/2
 ⠿ Container postgresdjango_db_1   Created                                                                                                                            0.0s
 ⠿ Container postgresdjango_web_1  Recreated                                                                                                                          2.5s
Attaching to db_1, web_1
db_1   | chmod: changing permissions of '/var/lib/postgresql/data': Operation not permitted
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
db_1 exited with code 1
web_1  | Watching for file changes with StatReloader
web_1  | Performing system checks...
web_1  |
web_1  | System check identified no issues (0 silenced).
web_1  | Exception in thread django-main-thread:
web_1  | Traceback (most recent call last):
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
web_1  |     self.connect()
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 33, in inner
web_1  |     return func(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 200, in connect
web_1  |     self.connection = self.get_new_connection(conn_params)
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 33, in inner
web_1  |     return func(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
web_1  |     connection = Database.connect(**conn_params)
web_1  |   File "/usr/local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
web_1  |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
web_1  | psycopg2.OperationalError: could not translate host name "db" to address: Temporary failure in name resolution
web_1  |
web_1  |
web_1  | The above exception was the direct cause of the following exception:
web_1  |
web_1  | Traceback (most recent call last):
web_1  |   File "/usr/local/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
web_1  |     self.run()
web_1  |   File "/usr/local/lib/python3.10/threading.py", line 946, in run
web_1  |     self._target(*self._args, **self._kwargs)
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
web_1  |     fn(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
web_1  |     self.check_migrations()
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 486, in check_migrations
web_1  |     executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/db/migrations/executor.py", line 18, in __init__
web_1  |     self.loader = MigrationLoader(self.connection)
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/db/migrations/loader.py", line 53, in __init__
web_1  |     self.build_graph()
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/db/migrations/loader.py", line 220, in build_graph
web_1  |     self.applied_migrations = recorder.applied_migrations()
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
web_1  |     if self.has_table():
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/db/migrations/recorder.py", line 55, in has_table
web_1  |     with self.connection.cursor() as cursor:
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 33, in inner
web_1  |     return func(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 259, in cursor
web_1  |     return self._cursor()
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 235, in _cursor
web_1  |     self.ensure_connection()
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 33, in inner
web_1  |     return func(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 218, in ensure_connection
web_1  |     with self.wrap_database_errors:
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/db/utils.py", line 90, in __exit__
web_1  |     raise dj_exc_value.with_traceback(traceback) from exc_value
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
web_1  |     self.connect()
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 33, in inner
web_1  |     return func(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 200, in connect
web_1  |     self.connection = self.get_new_connection(conn_params)
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 33, in inner
web_1  |     return func(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
web_1  |     connection = Database.connect(**conn_params)
web_1  |   File "/usr/local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
web_1  |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
web_1  | django.db.utils.OperationalError: could not translate host name "db" to address: Temporary failure in name resolution
web_1  |





links:
  - db:db

EDIT:编辑:

docker-copmpose.yaml码头工人-copmpose.yaml

version: "3.9"
   
services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    environment:
      - POSTGRES_NAME=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    depends_on:
      - db
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".

As you can see above, you have not set password.如上所示,您尚未设置密码。

You should set password in docker-compose.yml file.您应该在docker-compose.yml文件中设置密码。

version: "3.9"
   
services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    environment:
      - POSTGRES_NAME=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=YOUR_PASSWORD_HERE
    depends_on:
      - db

You should set password in database.ini file like this:您应该像这样在database.ini文件中设置密码:

[postgresql]
host=localhost
database=suppliers
user=postgres
password=SecurePas$1

Or you can use "docker run -e POSTGRES_PASSWORD=password" command.或者您可以使用“docker run -e POSTGRES_PASSWORD=password”命令。

Just add POSTGRES_PASSWORD to the db instance too.只需将 POSTGRES_PASSWORD 也添加到db实例中。

version: "3.9"
   
services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=postgres
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    environment:
      - POSTGRES_NAME=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    depends_on:
      - db

Could you show us the docker-compose content?你能告诉我们docker-compose的内容吗? There may be a problem with your network configuration.您的网络配置可能有问题。

暂无
暂无

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

相关问题 Docker django.db.utils.OperationalError:无法将主机名“db”转换为地址:名称解析暂时失败 - Docker django.db.utils.OperationalError: could not translate host name “db” to address: Temporary failure in name resolution django.db.utils.OperationalError:无法将主机名“db”转换为地址:名称解析暂时失败。 面向专业人士的 Django 书籍 - django.db.utils.OperationalError: could not translate host name "db" to address: Temporary failure in name resolution. Django for professionals book Django Docker:django.db.utils.OperationalError:无法将主机名“db”转换为地址:名称或服务未知 - Django Docker: django.db.utils.OperationalError: could not translate host name “db” to address: Name or service not known Docker 运行,在 docker-compose 构建后无法将主机名“db”转换为地址:名称或服务未知 - Docker run, after docker-compose build could not translate host name “db” to address: Name or service not known Django与Heroku上的Postgresql - 无法将主机名“db”转换为地址:名称或服务未知 - Django with Postgresql on Heroku - Could not translate host name “db” to address: Name or service not known 发生异常:OperationalError 无法将主机名“db”转换为地址:未知主机 - Exception has occurred: OperationalError could not translate host name “db” to address: Unknown host mLAb:名称解析暂时失败 - mLAb : Temporary Failure in name resolution 无法将主机名“postgres”转换为地址:未知主机 - could not translate host name “postgres” to address: Unknown host 无法使用 Postgres、Docker Compose 和 Psycopg2 将主机名“db”转换为地址 - Could not translate host name "db" to address using Postgres, Docker Compose and Psycopg2 通过 PyCharm 在 Docker 中运行 Django 测试时,无法将主机名“db”转换为地址 - could not translate host name "db" to address when running Django test in Docker through PyCharm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM