无法从docker executor(gitlab ci/cd)连接到PostgreSQL db

laik7k3q  于 12个月前  发布在  PostgreSQL
关注(0)|答案(1)|浏览(250)

我尝试了官方gitlab文档中解释的方式,在主机上配置数据库并尝试与DB_HOST=postgres连接。但是psycopg抛出错误:(psycopg.OperationalError) connection is bad: Name or service not known. My .gitlab-ci.yml:

image: python:3.11

default:
  services:
    - postgres:15.1

before_script:
  - python3.11 --version
  - pip3.11 --version
  - echo "hello world"

build-python:
  stage: build
  script:
    - echo "this is test job"
    - cd backend
    - pip3.11 install virtualenv
    - virtualenv venv
    - source venv/bin/activate
    - pip3.11 install -r requirements.txt
    - echo "ended building python test job"
    - alembic revision --autogenerate -m "migrating"
    - alembic upgrade head

字符串
Alembic.ini文件从我试图访问数据库:

# A generic, single database configuration.

[alembic]
# path to migration scripts
script_location = migrations

prepend_sys_path = .

version_path_separator = os  # Use os.pathsep. Default configuration used for new projects.

sqlalchemy.url = driver://user:pass@localhost/dbname

[post_write_hooks]

[loggers]
keys = root,sqlalchemy,alembic

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console
qualname =

[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine

[logger_alembic]
level = INFO
handlers =
qualname = alembic

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S


sqlalchemy.urlenv.py中定义

config.set_main_option('sqlalchemy.url', DATABASE_URL)


DATABASE_URL

DATABASE_URL = f'postgresql+psycopg://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}'


我错过了什么?谢谢你的帮助。

6l7fqoea

6l7fqoea1#

我猜PostgreSQL容器无法启动是因为它丢失了POSTGRES_PASSWORD必需的配置变量。有关此图像,请参见the documentation,环境变量部分。故障排除的一种方法是使用以下命令打印PostgreSQL容器的日志

variables:
  CI_DEBUG_SERVICES: 'true'

字符串
它将在作业日志中显示PostgreSQL容器的日志。
如果PostgreSQL容器正确启动,那么您应该能够使用postgres主机名访问它,如文档所示:
服务主机名的默认别名是根据其映像名称创建的,并遵循以下规则:

  • 冒号(:)之后的所有内容都将被删除。
  • 斜杠(/)将替换为双下划线(__),并创建主别名.
  • 斜杠(/)被替换为一个短划线(-),并创建了辅助别名(需要GitLab Runner v1.1.0或更高版本)。

相关问题