database.yml rails

Database.yml is one of the most important files in rails project, database.yml rails is actualy a configuration file.

What is Contains?

Database.yml is configuration file which tells rails about database, database information such as

Paramter
Description
host  URL of the Host Machine where database is installed. If over a network then IP/Web address and if local then 127.0.0.1
database  The name of database to be selected. Rails way is mydb_development
username  The username of the database chosen
password  password for the user account with given username to the database
adaptor  Type of database being used for the Rails application (can be any MySQL/Oracle/Sqlite... etc)
pooling  Related to maximum number of connections to be made with database

 

There are such multiple configurations here in this file. You need to write appropriate values in this configuration based on the Database you are using, and host/machine of the DB. If you want to use MySQL Database.yml :

development:
adapter:
mysql2
encoding: utf8
reconnect: true
database: [DB name]
pool: 5
username: [your username]
password: [your password]
host: [host name]

Now If you are a java programmer, you might land up with a question in your mind that what about the drivers/connectors required. In java we need JAR files depending upon the database we are using.Rails will automatically detect the drivers based on the adapter parameter in the yml file and connect appropriately. You just need to provide appropriate Jem which is equivalent to a Jar file. With the above yml file your project is good to proceed. Similarly you need to change some configurations to connect to some other database, such as Oracle. Oracle Database.yml:

adapter: oracle
database: comics_catalog_development
username: [your username]
password: [your password]
host: [host name]

postgres Database.yml:

development:
adapter:
postgresql
encoding: unicode
database: [DB name]
pool: 5
username: [your username]
password: [your username]

IBMDB2 Database.yml:

development:
adapter:
jdbc
driver: com.ibm.db2.jcc.DB2Driver
url: jdbc:db2://HOSTNAME:50000/
host: localhost
port: 50000
database: [DB name]
username: [your username]
password: [your password]

MSSQL Database.yml:

development:
adapter:
sqlserver
mode: ODBC
username: [your username]
password: [your password]
database: [DB name]
dsn: my_dev_server_ds

Rails has capability to connect to majority of the databases present today. You can also create database.yml with various environments such as you need some different database for development,testing,production as : 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
username: [your username]
password: [your password]
database: [DB name]
socket: /tmp/mysql.sock
host: [host machine]    
port: 3306

;