Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting the postgres service on Gitlab-CI

I have a Rails5 application that uses Postgres. I'm trying to use the Gitlab CI to run my spec tests when I push up new commits.

I tried setting up my file based on the documentation for configuring the .gitlab-ci.yml file and using the postgres service

I haven't changed my runner, so it's running with whatever default Gitlab uses.

.gitlab-ci.yml

services:
  - postgres:9.6

variables:
  POSTGRES_DB: galapagos_test
  POSTGRES_USER: runner
  POSTGRES_PASSWORD: ""

before_script:
  # System
  - apt-get update -qq

  # Ruby
  - ruby -v
  - which ruby

  # Gems
  - 'echo ''gem: --no-ri --no-rdoc'' > ~/.gemrc'
  - gem install bundler
  - bundle install --without production --jobs $(nproc) "${FLAGS[@]}"

  # App
  - RAILS_ENV=test bundle exec rake db:create
  - RAILS_ENV=test bundle exec rake db:migrate

rspec:
  script:
    - bundle exec rspec

database.yml

My Rails app database.yml is configured to use the same postgres database, username, and password specified above

development: &default
  adapter: postgresql
  database: galapagos_development
  encoding: utf8
  host: localhost
  min_messages: warning
  pool: 2
  timeout: 5000

test:
  <<: *default
  database: galapagos_test
  user: runner
  password: ""

The Error

When the build runs it keeps failing the rake task with

$ RAILS_ENV=test bundle exec rake db:create
could not connect to server: Connection refused
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?
Couldn't create database for {"adapter"=>"postgresql", "database"=>"galapagos_test", "encoding"=>"utf8", "host"=>"localhost", "min_messages"=>"warning", "pool"=>2, "timeout"=>5000, "user"=>"runner", "password"=>""}
rake aborted!
PG::ConnectionBad: could not connect to server: Connection refused
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?
/usr/local/bundle/gems/pg-0.20.0/lib/pg.rb:56:in `initialize'
/usr/local/bundle/gems/pg-0.20.0/lib/pg.rb:56:in `new'
/usr/local/bundle/gems/pg-0.20.0/lib/pg.rb:56:in `connect'

Clearly the postgres service is installed (the build logs indicate that 9.6 is installed) but the psql server isn't started.

How can I start the postgres service?

I tried 2 "common" solutions I've seen posted, but they both produce errors as well.

$ /etc/init.d/postgresql restart
/bin/bash: line 69: /etc/init.d/postgresql: No such file or directory
ERROR: Job failed: exit code 1


$ service postgresql start
postgresql: unrecognized service
ERROR: Job failed: exit code 1
like image 854
user2490003 Avatar asked Jun 22 '18 02:06

user2490003


1 Answers

Change host: localhost to host: postgres in your database.yml.

A service is another container that is not accessible locally. So you have to access it by it's hostname - the name of the service by default. You could set an optional alias for a service if necessary documentation

like image 175
Sascha Frinken Avatar answered Nov 04 '22 12:11

Sascha Frinken