3

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.

  • As an update, I found that using Ruby 2.6.5 and Postgres 10.1 seems to allow it to resolve the postgres server correctly. I'm not sure why, nor do I know how to fix it for Ruby 2.6.7/Postgres 12.3. – SitephenD77 May 25 '21 at 00:08
  • Can't you update? Ruby 2.6 was released way back in 2018 and will be EOL in less then a year. https://www.ruby-lang.org/en/downloads/branches/ – max May 25 '21 at 11:14
  • I suppose I could, but I'm not sure it would solve the problem, plus I'd like to know what's going on so I can fix it in the future if needed. – SitephenD77 May 25 '21 at 22:14

0 Answers0