英文:
Encountering psycopg.OperationalError: connection is bad: Temporary failure in name resolution, when attempting to run migrations using GitHub Actions
问题
目前正在尝试使用Django作为服务器和PostgreSQL作为数据库来实施项目的后端CI。我正在使用GitHub Actions自动化这个过程,以便在每次拉取请求时触发,但当它尝试执行迁移时,它会给我报错,标题中提到的错误。我暂时将其更改为在分支推送时运行,以便每次测试时都不必创建新的拉取请求。
这是.yml文件中相关部分的内容:
name: Backend CI
on:
push:
branches:
- test-branch
jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: server
services:
db:
image: postgres
env:
POSTGRES_HOST: db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: github-actions
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
...
- name: Run Migrations
env:
API_SECRET_KEY: secret-key
POSTGRES_HOST: db
POSTGRES_PASSWORD: password
POSTGRES_PORT: 5432
run: |
source .venv/bin/activate
python manage.py migrate
...
我预期迁移会顺利运行,但实际上它给我报了标题中的错误。
我到目前为止尝试过的方法有:
- 为数据库指定主机名
- 为服务器提供了连接的预期主机名和密码
有人告诉我要检查是否在容器中运行Django,我认为我是在容器中运行的?我之前的假设是Django和数据库将在同一个虚拟环境中运行。
英文:
Currently attempting to implement backend CI for a project using Django for the server and PostgreSQL for the database. I'm automating this using GitHub Actions to occur whenever a pull request is done, but when it tries to attempt migrations, it gives me the error mentioned in the title. I've temporarily changed it to run on a push to the branch so I don't have to make a new pull request every time I want to test it.
This is the relevant bits from the .yml file:
name: Backend CI
on:
push:
branches:
- test-branch
jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: server
services:
db:
image: postgres
env:
POSTGRES_HOST: db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: github-actions
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
...
- name: Run Migrations
env:
API_SECRET_KEY: secret-key
POSTGRES_HOST: db
POSTGRES_PASSWORD: password
POSTGRES_PORT: 5432
run: |
source .venv/bin/activate
python manage.py migrate
...
What I expected to happen was for migrations to run smoothly, but it instead throws me the error from the title.
What I've tried so far:
- Specifying hostname for the db
- Providing the expected hostname and password for the server to connect to
Someone I asked told me to check if I'm running Django in the container and I think I am? I was under the assumption that Django was running in the same virtual environment as the database would be.
答案1
得分: 0
奇怪的是,似乎有效的方法是取消指定主机名,我从服务块中删除了POSTGRES_HOST: db
,并在迁移部分的主机名中将db更改为localhost。
英文:
Strangely what seemed to work was unspecifying a hostname, I took out POSTGRES_HOST: db
from the services block and changed db to localhost in the host name under the migrations bit.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论