How to correctly setup a database.yml file in Rails 4
The second database.yml you posted is actually equivalent to the first, it just copies values from the development block.
To answer your other questions:
1) Should I be setting usernames and passwords in ALL environments
you can if you wish, or you can leave it as you have above where it takes the credentials all from one block.
2)If I'm using clear DB with Heroku as my database then should I be uncommenting
heroku actually completely disregards this file (which shouldn't be checked into source control in the first place anyway). Heroku has its own mechanism to handle databases which you can read more about here: https://devcenter.heroku.com/articles/heroku-postgresql
Essentially, treat "heroku's databases" and local databases that you define in this file completely different.
How to set up database.yml
The &default
in the line default: &default
, means make the key/value pairs under this YAML namespace available as the variable *default
.
The <<: *default
lines are taking those key/value pairs, and making them part of the other groups, so you only need to change values that are different from the default:
group.
If all your databases are on the same machine, then your database.yml
should look a lot like this:
default: &default
host: 111.222.333.444
adapter: sqlserver
mode: odbc
dsn: DSN_Name
username: sa
password: ********
development:
<<: *default
database: ERP_development
test:
<<: *default
database: ERP_test
production:
<<: *default
database: ERP_production
Since you are on sqlserver, you'll probably have to create all those databases yourself (instead of letting Rails do it for you).
How do I configure database.yml so that rake db:create creates multiple databases?
You can create individual databases with
RAILS_ENV=staging rake db:create
How to configure database.yml for deployment to Heroku
For Heroku you will have to use postgresql as it doesn't support mysql2. Heroku has its own mechanism to handle databases which you can read more about here: https://devcenter.heroku.com/articles/heroku-postgresql
Essentially, treat "heroku's databases" and local databases that you define in this file completely different. It will be easier for you to use sqlite for local and test environments and for production you should change your yaml code to this :
development:
adapter: mysql2
encoding: utf8
database: my_app_development
pool: 5
username: root
password:
test:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
production:
adapter: postgresql
database: my_database_production
pool: 5
timeout: 5000
Above code is not enough to get it working on heroku yet, you will also need to edit the gemfile content like below :
gem 'pg', :group => :production
gem 'mysql2' , :group => :development
gem 'sqlite3', :group => :test
I have made the gemfile code according to the database.yaml code that i wrote. You can use mysql2 for both development and test environment. If you are doing so you can change the gemfile contents like below:
gem 'pg', :group => :production
gem 'mysql2' , :group => [:development, :test]
Hope this helps.. :)
How to configure database.yml to work in a local environment and in Docker both?
Use the ENV vars Luke.
ENV['DATABASE_URL']
is merged with the settings from database.yml
and takes precedence over the values from file.
To avoid developer wars database.yml
should only contain the bare minimum of settings such as the adapter, encoding and the default names of the DBs:
default: &default
adapter: postgresql
encoding: unicode
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
Anything else such as hosts, passwords should be set by ENV['DATABASE_URL']
as this is local configuration - not application configuration. This also is preferable for security reasons as it removes the risk of passwords being leaked with the source code.
See:
- http://guides.rubyonrails.org/configuring.html#configuring-a-database
- https://12factor.net/config
How to setup database.yml to connect to Postgres Docker container?
You could set a correct environment variable first, and access it from your database.yml
:
host: <%= ENV['POSTGRES_IP'] %>
With a bashrc like (using bash substring removal):
export DOCKER_HOST=$(docker-machine env default)
export POSTGRES_IP=${DOCKER_HOST#tcp://}
Where is defined path to database.yml in rails?
It is hardcoded to be <app-root>/config/database.yml
. In production environment where you store it in a shared folder, you must create a symlink that points to that shared file.
<app-root>/config/database.yml -> ~/sites/shared/config/database.yml
Or you could symlink the whole config directory, if you wanted.
Related Topics
Exporting an Environment Variable in Ruby
Undefined Method 'Visit' When Using Rspec and Capybara in Rails
Long Running Delayed_Job Jobs Stay Locked After a Restart on Heroku
Idiomatic Object Creation in Ruby
Rails Sends 0 Byte Files Using Send_File
Method and Variable Name Is the Same
How to Implement a Friendship Model in Rails 3 for a Social Networking Application
Double Pipe Symbols in Ruby Variable Assignment
Best Practices to Handle Routes For Sti Subclasses in Rails
Devise Custom Routes and Login Pages
String.Force_Encoding() in Ruby 1.8.7 (Or Rails 2.X)
Can't Access Rubygems - Possibly Due to Ssl
Why Doesn't My Cron Job Work Properly