Skip to main content

database.yml rails

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
1. database host/machine
2. which database to choose ( mySQL, Oracle, Sqlite... etc)
3. Username
4. password
.
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

Comments

Popular posts from this blog

Error malloc(): memory corruption nginx with passenger?

Error malloc(): memory corruption nginx with passenger Passenger issue resolving steps :  sudo gem uninstall passenger(uninstall all passenger) sudo gem install passenger sudo passenger-install-nginx-module --auto --auto-download --prefix=/opt/nginx --extra-configure-flags=none Update nginx config file with new passenger version and restart the nginx

Lazy loading in rails – Rails Feature

 Lazy loading in rails – Rails Feature ? Lazy loading in rails is the amazing feature provided with rails. In console you might have tried to examine how lazy loading in rails actually works. In this tutorial, we will learn about this Rails - Lazy loading feature with examples. What exactly is Lazy Loading? As the name suggests the data is loaded in lazy manner (Really!) i.e. Your database is queried only when data from the database is required for some kind of manipulation in code. You will get more of this after you read how-to of lazy loading below. How lazy loading works: Whenever you try to get some data from database, For example, users is the database table that you have. And you are querying database to get users having age less than 20. Then, you will write code like, result = User.where("age < 20") when above statement is executed, your database is not queries yet(because the resultant data is not required yet). When you execute following code, records = resu...

Rails Migration Difference between Text and String

Rails Migration Difference between Text and String ? While working with Rails Migration Difference between Text and String is important to be known to every developer. Columns and their data types are finalized while deciding Table structure. This tutorial will help understand difference between String and Text column type and illustrate how to write Rails Migration implementing the same. You might want to read about database.yml files for specifying database configuration for Rails Application. 1. Concepts When String or Text data type is required?     Whenever you require your column to store information which is lengthy in size (Many characters), you need to consider String or Text data type for the column.     Both of them let you store Many(How Many - will see later) characters Difference between String and Text Considering MySQL database Feature     String     Text Length     1 to 255     ...