我尝试了官方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.url
在env.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}'
型
我错过了什么?谢谢你的帮助。
1条答案
按热度按时间6l7fqoea1#
我猜PostgreSQL容器无法启动是因为它丢失了
POSTGRES_PASSWORD
必需的配置变量。有关此图像,请参见the documentation,环境变量部分。故障排除的一种方法是使用以下命令打印PostgreSQL容器的日志字符串
它将在作业日志中显示PostgreSQL容器的日志。
如果PostgreSQL容器正确启动,那么您应该能够使用
postgres
主机名访问它,如文档所示:服务主机名的默认别名是根据其映像名称创建的,并遵循以下规则: