我有一个使用Postgres的Rails5应用程序。当我推送新的提交时,我试图使用Gitlab CI来运行我的规格测试。
我尝试根据configuring the .gitlab-ci.yml file和using the postgres service的文档设置文件
我尚未更改跑步者,因此它可以在Gitlab使用的任何默认设置下运行。
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
我的Rails应用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服务器没有启动。
如何启动postgres服务?
我尝试了2种常见的解决方案,但它们也会产生错误。
$ /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
答案 0 :(得分:1)
将host: localhost
中的host: postgres
更改为database.yml
。
服务是另一个无法在本地访问的容器。因此,您必须通过主机名来访问它-默认情况下是服务的名称。您可以根据需要设置服务的可选别名documentation