python - Postgres 错误无法将主机名“postgres”转换为地址:名称或服务未知
问题描述
我的 GitLabCI:
stages:
- test
# jobs
test_django:
stage: test
image: python:3.6 # this pulls the standard python 3.6 docker image
services:
- postgres:9.5 # this pulls a docker image with pgsql on it
variables:
# env vars made available to the script below
CI_DEBUG_TRACE: "false" # set to false to reduce the debug output from the CI jobs
POSTGRES_DB: "project_ci_test"
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: ""
DATABASE_NAME: "$POSTGRES_DB"
DATABASE_HOST: "postgres" # magically finds the service above
DATABASE_USER: "postgres"
DATABASE_PASSWORD: ""
DJANGO_SECRET_KEY: "somerandombogusstringtokeepdjangohappyandstartingproperly"
script:
- apt-get update -qy
- apt-get install -y python3-pip wkhtmltopdf xvfb # extra packages used for pdf rendering in the tests
- pip install -r requirements.txt
- pep8 . --filename=*.py --ignore=E128,E265,E501 --repeat
- python manage.py migrate
- python manage.py test
但是现在我在运行迁移时遇到了错误。
GitlabCi 错误:
self.connection = self.get_new_connection(conn_params)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection
connection = Database.connect(**conn_params)
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 translate host name "postgres" to address: Name or service not known
ERROR: Job failed: exit code 1
你好,伙计们,你能帮帮我吗?我的设置有什么问题,几个月前它可以工作,但不是它不工作。
Django 数据库设置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ.get('DATABASE_NAME', ''),
'USER': os.environ.get('DATABASE_USER', ''),
'PASSWORD': os.environ.get('DATABASE_PASSWORD', ''),
'HOST': os.environ.get('DATABASE_HOST', '127.0.0.1'),
'PORT': '5432',
}
}
解决方案
几个小时前我遇到了这个问题,我的设置与你的有点不同,但你可以在运行脚本之前尝试连接到 postgres,然后将数据库作为脚本中的第一件事导出。你会有这样的东西:
variables:
...
POSTGRES_DB: project_ci_test
POSTGRES_USER: postgres
POSTGRES_PASSWORD: somerandompassword
connect:
image: postgres
script:
- export PGPASSWORD=$POSTGRES_PASSWORD
- psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT 'OK' AS status;"
script:
- export DATABASE_URL=postgres://postgres:somerandompassword@postgres:5432/project_ci_test
...
获取向 psql 提供密码的官方方式:这里
推荐阅读
- python - Pandas 中的 Shift 分组系列
- bash - 在 shell 集中使用 shell 变量
- python - Pandas:CSV 标题和数据行大小不匹配
- python - 如何在 Jupyter Notebook 的 API 有效负载中使用变量
- selenium - 无法将测试执行结果传递给 Robo 框架中的变量
- angular - 当我从 web 服务填充数据时,Angular6 和 ng2-charts 不显示任何图表
- mysql - mysql全文通配符搜索,如正则表达式
- c# - c#中的预处理技术
- python - Python - 数据清洗
- arrays - Applescript 比较同一数组中的项目