Gitlab-CI can't connect to postgres - "Is the server running on host "postgres" (127.0.0.1)?"

Viewed 463

I'm trying to set up CI for my Rails project. Gitlab is self-hosted and I'm also self-hosting the runner on k3s. When trying to create the database, I get the following error:

bin/rails:4:in `<main>'
Caused by:
PG::ConnectionBad: could not connect to server: Connection refused
    Is the server running on host "postgres" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?

I've found dozens of posts saying to change localhost to postgres, which I've done. I have no idea why it's resolving postgres to 127.0.0.1 though.

.gitlab-ci.yml:

image: "ruby:2.6.7"

services:
  - postgres:12.3

before_script:
  - ruby -v
  - cp config/database.yml.ci config/database.yml
  - apt-get update -q && apt-get install nodejs -yqq
  - gem install bundler:2.0.2
  - bundle install -j $(nproc) --path vendor

rails:
  script:
    - bundle exec rails db:create RAILS_ENV=test
    - bundle exec rails db:schema:load RAILS_ENV=test
    - bundle exec rails db:seed
    - bundle exec rails test
    - bundle exec webpack

database.yml.ci:

default: &default
  adapter: postgresql
  encoding: unicode
  username: postgres
  password: postgres
  host: postgres
  pool: 5
  database: ci_db

test:
  <<: *default
  database: ci_Test

I have tried different versions of postgres and ruby.

0 Answers
Related