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 theRails 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?
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' %>
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.