Starting the postgres service on Gitlab-CI - ruby-on-rails

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

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

Related

Rails not able to access Postgresql deployed by Docker

I have a docker container that spins up my Postgres DB, Elasticsearch, and Redis; however, my rails server is unable to connect to the Postgres DB... The server starts, but when it attempts to connect to the DB, I get the error
PG::ConnectionBad (could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
):
When Docker launches the DB, it does so on localhost:5432. Via the terminal, I'm able to connect to the DB by running psql -h localhost -U myUser, but running psql results in the same error...
My rails database.yml file is configured to connect to the database at localhost:5432 with myUser, but I cannot figure out why it is giving me this error. It seems like Rails is attempting to connect to my Postgres.app server instead of the server Docker is running.
Here is my database.yml file
default: &default
adapter: postgis // I have tried "postgresql" as well
encoding: unicode
reconnect: true
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
url: <%= ENV['DATABASE_URL'] %>
development:
<<: *default
database: my_db
username: <my username>
password: <my password>
Here is my .env
DATABASE_URL=postgres://***:***#novus-postgis-localdev:5432
My docker container shows the following
ENVIRONMENT:
DATABASE_URL: postgres://<my username>:<my password>#novus-postgis-localdev:5432
PATH:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
...
PORTS:
5432/tcp: 0.0.0.0:5432
5432/tcp: :::5432
Therefore, my Rails environment is supposed to connect to my docker DB server, but it appears to be attempting to connect to my Postgres.app server (which isn't running).
What am I missing here / what is wrong with my configurations that have Rails looking at the wrong server?
FWIW, I'm on macOS and I installed Postgres via the Postgres.app.
When the Postgres.app is running a server, the psql command connects to it.
When the Docker container is running, I am unable to start the Postgres. App server because port 5432 is already used (as expected)
UPDATE
I did find out that my Docker DB container is listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" whereas my Rails is pointing to the socket /tmp/.s.PGSQL.5432. I am unable to figure out how to change either of these to be the same (rails to look at /var/run/postgresql/... or docker to launch socket at /tmp/...
In the Postgresql configuration file, try and enter,
listen_addresses='*';

PG::ConnectionBad - could not connect to server: Connection refused on Mac OS X

After installing the needed ruby version with: rvm install 2.7.1 and installing all the gems,
I was planning to create the db with rails db:create but I am getting this error:
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5442?
Couldn't create 'development' database. Please check the configuration.
rails aborted!
PG::ConnectionBad: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5442?
My database.yml
default: &default
adapter: postgresql
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 15 } %>
host: <%= ENV.fetch("DATABASE_HOST") { '127.0.0.1' } %>
development:
<<: *default
database: development
test:
<<: *default
database: test
production:
<<: *default
database: production
Seems that it's related to postgres.
It's version is: postgres (postgreSQL) 12.4
pg gem version is 1.2.3
Restarting the postgresql didn't helped
It appeared that I needed to create a new PostgreSQL database cluster.
First you need to remove the postgres dir in the /usr/local/var/
you can use this code - cd /usr/local/var/ && rm -rf postgres
after that - create the new postgres folder mkdir postgres (inside /usr/local/var)
and initialise the creation of the new PostgreSQL database cluster with -
cd && initdb --locale=C -E UTF-8 /usr/local/var/postgres
The last thing, restart the postgres - brew services restart postgresql

Hi I am trying to run rails db:create in WSL Ubuntu and keep getting this error in regards to postgres

This is my first time trying to set up postgres with rails and have tried so many different ways to get this working. Please help!
Error:
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 'backend_development' database. Please check your configuration.
rails aborted!
PG::ConnectionBad: 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?
database.yml:
default: &default
adapter: postgresql
encoding: unicode
username: postgres
password: password
host: localhost
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
When I run psql i receive this error again:
psql: error: could not connect to server: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
Do you have PostgreSQL installed properly?
To check the version
$ postgres --version .
If it's not installed on your system run
$ sudo apt-get install postgresql postgresql-contrib libpq-dev
If you have it installed login with
$sudo su - postgres
Then create a new user after logged in
$ createuser --pwprompt username
now create the database
$ createdb -O username database_name .

Rails does not consider `DATABASE_URL` in the `test` environment?

I'm using docker-compose to create two services for my Rails application:
web - the actual Rails/web app
db - A postgres container running postgres 11.5
I'm running both services locally with RAILS_ENV=development.
As a one-time manual step I want to The very first time I try to run rake db:create to create the database. However, I get a postgres connection error:
docker-compose up --build --no-start
docker-compose run --rm web bundle exec rake db:create
RAILS_ENV=development bundle exec rake db:create
Created database 'feeder_development'
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?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
Couldn't create 'feeder_test' database. Please check your configuration.
rake aborted!
PG::ConnectionBad: 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?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
/app/vendor/bundle/ruby/2.5.0/gems/pg-0.18.4/lib/pg.rb:45:in `initialize'
/app/vendor/bundle/ruby/2.5.0/gems/pg-0.18.4/lib/pg.rb:45:in `new'
/app/vendor/bundle/ruby/2.5.0/gems/pg-0.18.4/lib/pg.rb:45:in `connect'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:692:in `connect'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:223:in `initialize'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:48:in `new'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:48:in `postgresql_connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:811:in `new_connection'
As you can see, it successfully created the development database, but failed on creating the test database
(The db:create tasks automatically creates test and development databases together even when the environment is development)
Here is my database.yml.
default: &default
adapter: postgresql
encoding: utf8
host: localhost
min_messages: warning
pool: 5
timeout: 5000
test:
<<: *default
database: feeder_test
development:
<<: *default
database: feeder_development
According to the Rails guides, you can override the above config by specifying a DATABASE_URL, which I set as:
DATABASE_URL=postgres://feeder:password123#db/
So from what I can tell -
Rails merges the DATABASE_URL info with the database.yml info when trying to create the development database. All is good.
Rails does NOT consider the DATABASE_URL at all when creating the test database. It uses ONLY the database.yml info, which is why it's looking for a psql connection on localhost.
So - is there a way to get Rails to respect my DATABASE_URL ENV on the test environment?
in your database.yml set url to ENV['DATABASE_URL'] and leave other keys as defaults:
test:
url: <%= ENV['DATABASE_URL'] %>
database: app_test
host: 127.0.0.1
DATABASE_URL='postgresql://192.168.0.2/' rails runner 'puts ActiveRecord::Base.configurations.to_json' | jq '.test'
{
"database": "app_test",
"host": "192.168.0.2",
"adapter": "postgresql"
}
Notice that database name left unchanged but host has been overwritten.
I'm pretty sure you can put ERB into the database.yml file, so I'd suggest changing test to
test:
<<: *default
database: <%= ENV['DATABASE_URL'] || 'feeder_test' %>

bundle exec rake db:create error

Keep getting this when I run 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?
Any ideas how to fix this?
Thats probably because of a permission issue. Navigate to /etc/postgresql/<your version of pg>/main/ and open up pg_hba.conf.
There search for a line that'd say
local all postgres peer and replace it to local all all trust.
Then run sudo service postgresql restart in your terminal.
Try this :
It will work
your database.yml should look like this :
development:
adapter: postgresql
encoding: unicode
database: <DATABASE_NAME>
pool: 5
username: <username>
password: <password>
host: localhost
port: 5432
And it will be same for another environments

Resources