# Configurations

Just about every Rails application will interact with a database. You can connect to the database by setting an environment variable ENV['DATABASE_URL']or by using a configuration file called config/database.yml.

Since there are two ways to configure your connection (using config/database.yml or using an environment variable) it is important to understand how they can interact.

If you have an empty config/database.yml file but your ENV['DATABASE_URL'] is present, then Rails will connect to the database via your environment variable

If you have a config/database.ymlbut no ENV['DATABASE_URL']then this file will be used to connect to your database

  • DATABASE.YML

    The config/database.yml 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.

    If you wish, you can manually specify a URL inside of your config/database.yml

    development:
      url: postgresql://localhost/blog_development?pool=5
    
    1
    2

    The config/database.ymlfile can contain ERB tags <%= %>. Anything in the tags will be evaluated as Ruby code. You can use this to pull out data from an environment variable or to perform calculations to generate the needed connection information.

    You don't have to update the database configurations manually. If you look at the options of the application generator, you will see that one of the options is named --database. This option allows you to choose an adapter from a list of the most used relational databases. You can even run the generator repeatedly: cd .. && rails new blog --database=mysql. When you confirm the overwriting of the config/database.ymlfile, your application will be configured for MySQL instead of SQLite. Detailed examples of the common database connections are below.

  • Configuring SQLite3 database

    Rails comes with built-in support for SQLite3 (opens new window), 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
    
    1
    2
    3
    4
    5
  • Configuring MySQL or MariaDB Database

    If you choose to use MySQL or MariaDB instead of the shipped SQLite3 database, your config/database.ymlwill look a little different. Here's the development section:

    development:
      adapter: mysql2
      encoding: utf8mb4
      database: blog_development
      pool: 5
      username: root
      password:
      socket: /tmp/mysql.sock
    
    1
    2
    3
    4
    5
    6
    7
    8

    If your development database has 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.

    Advisory Locks are enabled by default on MySQL and are used to make database migrations concurrent safe. You can disable advisory locks by setting advisory_locks to false:

    production:
      adapter: mysql2
      advisory_locks: false
    
    1
    2
    3
  • Configuring PostgreSQL Database

    Finally, you’ll want to install the pg gem so that you can interface with Postgres from Ruby code. To do so:

    gem install pg
    
    1

    To create a Rails app configured for Postgres, run this command:

    rails new myapp --database=postgresql
    
    1

    the config generated will be like :

    default: &default
      adapter: postgresql
      encoding: unicode
      pool: 5
      host: localhost
      port: 5432
    
    development:
      <<: *default
      database: app_dev
      username: myapp
      password: password1
    
    test:
      <<: *default
      database: app_test
      username: myapp
      password: password1
    
    production:
      <<: *default
      database: app_production
      username: app
      password: password1
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
  • configuring mongoDB

    Create a new rails application to use Ruby MongoDB. Make sure that you add –skip-active-record.

    rails new my_mongo_app --skip-active-record
    
    1

    If you notice, there is no database.yml and no sqlite3 gem is added automatically. Now we have to add two gems which will be a bridge for us between Rails and MongoDB.

    Add the following gems to Gemfile.

    gem 'mongoid', '~> 6.0'
    gem 'bson_ext'
    
    1
    2

    Now do bundle install. Now we have to generate mongoid.yml file which is similar to database.yml file for us.

    Run the following command to generate MongoDB configuration files.

    rails g mongoid:config
    
    1

    Now update mongoid.yml  file based on your MongoDB configurations and create a database with rake db:create

Creating Database

Once the configuration is set you can create database with following commands

bin/rails db:create
1