首页 文章

在Gitlab-CI上启动postgres服务

提问于
浏览
1

我有一个使用Postgres的Rails5应用程序 . 当我推高新提交时,我正在尝试使用Gitlab CI来运行我的规范测试 .

我尝试根据configuring the .gitlab-ci.yml fileusing the postgres service的文档设置我的文件

我没有更改我的跑步者,所以它正在运行任何默认的Gitlab使用 .

.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 配置为使用上面指定的相同postgres数据库,用户名和密码

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: ""

错误

当构建运行时,它会使 rake 任务失败

$ 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'

显然安装了postgres服务(构建日志表明安装了 9.6 )但是psql服务器没有启动 .

How can I start the postgres service?

我尝试了两个我见过的“普通”解决方案,但它们都会产生错误 .

$ /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

1 回答

  • 1

    database.yml 中将 host: localhost 更改为 host: postgres .

    服务是另一个无法在本地访问的容器 . 所以你必须通过它的主机名来访问它 - 默认情况下是服务的名称 . 如有必要,您可以为服务设置可选别名documentation

相关问题