How do I set up the database.yml file in Rails?
The database.yml is the file where you set up all the information to connect to the database. It differs depending on the kind of DB you use. You can find more information about this in the Rails Guide or any tutorial explaining how to setup a rails project.
The information in the database.yml file is scoped by environment, allowing you to get a different setting for testing, development or production. It is important that you keep those distinct if you don't want the data you use for development deleted by mistake while running your test suite.
Regarding source control, you should not commit this file but instead create a template file for other developers (called database.yml.template
). When deploying, the convention is to create this database.yml file in /shared/config
directly on the server.
With SVN: svn propset svn:ignore config "database.yml"
With Git: Add config/database.yml
to the .gitignore file or with git-extra git ignore config/database.yml
... and now, some examples:
SQLite
adapter: sqlite3
database: db/db_dev_db.sqlite3
pool: 5
timeout: 5000
MYSQL
adapter: mysql
database: my_db
hostname: 127.0.0.1
username: root
password:
socket: /tmp/mysql.sock
pool: 5
timeout: 5000
MongoDB with MongoID (called mongoid.yml, but basically the same thing)
host: <%= ENV['MONGOID_HOST'] %>
port: <%= ENV['MONGOID_PORT'] %>
username: <%= ENV['MONGOID_USERNAME'] %>
password: <%= ENV['MONGOID_PASSWORD'] %>
database: <%= ENV['MONGOID_DATABASE'] %>
# slaves:
# - host: slave1.local
# port: 27018
# - host: slave2.local
# port: 27019
The database.yml
is a file that is created with new rails applications in /config
and defines the database configurations that your application will use in different environments. Read this for details.
Example database.yml:
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: mysql
encoding: utf8
database: your_db
username: root
password: your_pass
socket: /tmp/mysql.sock
host: your_db_ip #defaults to 127.0.0.1
port: 3306