Ruby on rails, connecting to postgresql to run rspec test


#1

Hi. I’m trying to run my rspec test. However, I’m unable to connect to the postgres database. I’m using the official ruby docker image. On the services tab, i have indicated the posgresql database.

The commands that i used were:

psql -v
gem install bundler
bin/bundle install
rails db:reset
rspec 

The error that i got:

Output 3: psql: not found
Failed Build failed !!!.


#2

Hi @koh,

Postres is not installed within Ruby official image, so what you need to do is to go to ‘Setup commands’ in this action and add:

apt-get update -y apt-get install postgresql-9.4 -y

That should do the trick!


#3

Hi @Octavia. I updated my commands to:

apt-get update -y
apt-get install postgresql-9.4 -y
gem install bundler
bin/bundle install
rspec

But i’m still getting this error. Thanks for your help.
> Bundle complete! 35 Gemfile dependencies, 137 gems now installed.
> Bundled gems are installed into /usr/local/bundle.
> Execute rspec
> Output Coverage report generated for RSpec to /spf-ocsp/coverage. 19 / 242 LOC (7.85%) covered.
> /usr/local/bundle/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/postgresql_adapter.rb:671:in `initialize’: could not connect to server: Connection refused (PG::ConnectionBad)
> 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?


#4

@koh, from what you’ve pasted above, I can see that you’re trying to connect to the 127.0.0.1 localhost.
Upon adding the service, you have the following host given (see: screenshot)


#5

Hi, Octavia! Could your please help me with these rails + postgres settings? I’m struggling for hours with that.
I’m setting my database.yml to use postgres as the host, like it is defined in the postgres service I created but I keep receiving the error message it was trying to connect using localhost/127.0.0.1:

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?


#6

HI,
I’ve had same issue as you.
After adding postgres service, I forgot to tell rails to load test db only
here is my action script

bundle
RAILS_ENV=test rails db:schema:load
RAILS_ENV=test rspec

Hope that helps