I tried the way explained in official gitlab documentation, configured database on the host and tried to connect with DB_HOST=postgres
. But psycopg
throws error: (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 file from where I'm trying to access db:
# 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
is defined in env.py
config.set_main_option('sqlalchemy.url', DATABASE_URL)
The DATABASE_URL
:
DATABASE_URL = f'postgresql+psycopg://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}'
What did I miss to do? Thanks for any help.
-
) to ensure all commands run in the same shell. Second, where’s the Alembic config which configures the connection to the db?DB_xxx
environment variables? You ought to be able to connect tolocalhost:5432
. Also, which PG driver do you use, v2 or v3? I usually usepostgresql+psycopg2
. Also, migrations should be part of the source, not generated in CI.