Configuring the Database in Ruby on Rails

Configuring a Database

Just about every Rails application will interact with a database. The database to use is specified in a configuration file, config/database.yml. If you open this file in a new Rails application, you’ll see a default database configured to use SQLite3. The file contains sections for three different environments in which Rails can run by default:

  • The development environment is used on your development/local computer as you interact manually with the application.
  • The test environment is used when running automated tests.
  • The production environment is used when you deploy your application for the world to use.

1) Configuring an SQLite3 Database

Rails comes with built-in support for SQLite3, which is a lightweight serverless database application. While a busy production environment may overload SQLite, it works well for development and testing. Rails defaults to using an SQLite database when creating a new project, but you can always change it later.

Here’s the section of the default configuration file (config/database.yml) with connection information for the development environment:

  development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

In this guide we are using an SQLite3 database for data storage, because it is a zero configuration database that just works. Rails also supports MySQL and PostgreSQL “out of the box”, and has plugins for many database systems. If you are using a database in a production environment Rails most likely has an adapter for it.

2) Configuring a MySQL Database

If you choose to use MySQL instead of the shipped SQLite3 database, yourconfig/database.yml will look a little different. Here’s the development section:

  development:
  adapter: mysql
  encoding: utf8
  database: new_development
  pool: 5
  username: root
  password:
  socket: /tmp/mysql.sock

If your development computer’s MySQL installation includes a root user with an empty password, this configuration should work for you. Otherwise, change the username and password in the development section as appropriate and you can also do these changes in test and production sections too.

3) Configuring a PostgreSQL Database

If you choose to use PostgreSQL, your config/database.yml will be customized to use PostgreSQL databases:

  development:
  adapter: postgresql
  encoding: unicode
  database: new_development
  pool: 5
  username: blog
  password:

You can also do these changes in test and production sections.

Change the username and password in the development section as appropriate.

Creating the Database

Now that you have your database configured, it’s time to have Rails create an empty database for you. You can do this by running a rake command:

$ rake db:create

This will create your development and test  databases inside the db/ folder

2 thoughts on “Configuring the Database in Ruby on Rails

Leave a Reply

Your email address will not be published. Required fields are marked *