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' %>
Related
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
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
I have created DB instance and gone through the config process of aws rds instance as per the documentation.
created an instance of db successfully.
also changed the connection params in database.yml accordingly.
I ran the following commands in ngnix server installed on ubuntu 16.04 at aws and also rebooted the db instance several times.
RAILS_ENV=production rake db:create
and db created successfully
and then ran
RAILS_ENV=production rake db:migrate
and database migrated successfully.
an then ran the command
sudo service nginx restart
after all of the process
when I am visiting my app its says
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?
here is my database.yml
development:
host: localhost
database: ups_developement_db
adapter: postgresql
encoding: unicode
port: 5432
username: postgres
password: postgres
production:
host: dbcustom.cjocvmnblsa4.us-east-2.rds.amazonaws.com
database: ups_db
adapter: postgresql
encoding: unicode
port: 5432
username: dbmasteruser
password: dbmasterPassword
I am open to provide any kind of information required.
Thanks a lot for your time and solution in advance
It's almost certain that you have not configured the security groups on the RDS instance correctly.
Log into your EC2 and issue the command
telnet <rds hostname> 5432
You should see something like
Trying 10.1.1.2...
Connected to 10.1.1.2.
Escape character is '^]'.
which means that TCP can flow between your EC2 and your RDS.
If not, you need to permit your EC2 to connect to your RDS instance by adding the correct security group configuration.
You should create Environment variables in your AWS RDS instance you should use them in your rails app config file. Check here
database.yml should look like this
production:
adapter: mysql2
encoding: utf8
database: <%= ENV['RDS_DB_NAME'] %>
username: <%= ENV['RDS_USERNAME'] %>
password: <%= ENV['RDS_PASSWORD'] %>
host: <%= ENV['RDS_HOSTNAME'] %>
port: <%= ENV['RDS_PORT'] %>
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
I have an ec2 server and rds server.
and a Ruby On Rails App
connecting to the rds with these settings worked for me in local ENV:
host: myappnameandhash.us-west-2.rds.amazonaws.com
adapter: mysql2
encoding: utf8
reconnect: false
database: mainDb
pool: 20
username: root
password: xxxx
socket: /var/run/mysqld/mysqld.sock
port: 3306
but on my EC2 server I don't have that mysqld.sock file
so i get this error:
FATAL: failed to connect to MySQL (error=Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2))
what do i need to install in order to have the socket?
thanks
update:
I removed the socket definition and the port.
I deploy using capistrano , now i ssh to my server and go to the "current" folder. there i try to run: rake ts:start
and i get the following:
rake aborted!
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
but i don't even have the socket definition in my database.yml file anymore
Remove the socket and port number from your database.yml file and then try, it will work.
You must specify the "host" of your RDS, and also configure the security groups in the RDS to allow your instance to connect to it.
I know this has been a while, just encountered this issue with my EC2 server connecting to RDS. Would like to share how I resolve this.
First removing socket and port from the database.yml help
#In your database.yml
development:
#some configuration
staging:
#some configuration
production:
adapter: mysql2
encoding: utf8
reconnect: false
pool: 20
host: url.to.rds.server
database: your-database
username: your-username
password: your-password
To run this rake command on production, you need to specify your rails-env, e.g. if you are using production environment, it should be
bundle exec rake ts:start RAILS_ENV=production