Open In App

Ruby on Rails - Database Setup

Last Updated : 18 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Ruby and Rails is a powerful web software framework that simplifies database control and interactions. right database setup is crucial for any Rails utility, as it guarantees efficient information garage and retrieval. This guide will walk you through putting in databases on your Rails application, specializing in famous databases like MySQL and PostgreSQL. whether you’re beginning a brand new undertaking or configuring a present one, knowledge of the database setup technique in Rails is crucial for building strong and scalable programs.

Database Setup for MySQL

Step1: Installing MySQL

Before setting up MySQL with Rails, you would need to have MySQL installed on your system. Here is how to install it:

On macOS: Use Homebrew to install MySQL.

brew install mysql

MySQL installation using Homebrew on macOS
MySQL installation using Homebrew on macOS

On Ubuntu: Install MySQL using APT.

sudo apt-get install mysql-server

After installation, start the MySQL service:

brew services start mysql

Commands for creating MySQL databases
Commands for creating MySQL databases

Step2: Creating a MySQL Database

Create a Database for Your Rails Application, you can refer: How to Connect MySQL with Ruby on Rails?

To create a database for your Rails application, log in to the MySQL console and fire up the following commands:

mysql -u root -p

Adding and installing the MySQL gem for Rails
Adding and installing the MySQL gem for Rails

//sql
CREATE DATABASE my_rails_app_development;
CREATE DATABASE my_rails_app_test;
CREATE DATABASE my_rails_app_production;

Creating and migrating the database in Rails
Creating and migrating the database in Rails

Step3: Configuring Rails to Use MySQL

Setup your application to use the MySQL database by editing config/database.yml:

//yaml
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: <%= ENV['MYSQL_PASSWORD'] %>
host: localhost

development:
<<: *default
database: my_rails_app_development

test:
<<: *default
database: my_rails_app_test

production:
<<: *default
database: my_rails_app_production
username: my_app_user
password: <%= ENV['MY_APP_DATABASE_PASSWORD'] %>

Installing PostgreSQL using Homebrew on macOS
Installing PostgreSQL using Homebrew on macOS

Write this SQL command to see Databases.

SHOW DATABASES;

Commands for creating PostgreSQL databases and users
Commands for creating PostgreSQL databases and users

Step 4: Installing the MySQL Gem

Add the mysql2 gem to your Gemfile to enable Rails to communicate with MySQL:

gem 'mysql2'

imresizer-1725246675560
Rails configuration for PostgreSQL

Then, run bundle install to install the gem.

bundle install

Creating and migrating the PostgreSQL database in Rails
Creating and migrating the PostgreSQL database in Rails

Step 5: Running Migrations

Finally, create the database and run the migrations to set up the database schema:

rails db:create
rails db:migrate

Creating and migrating the PostgreSQL database in Rails
Creating and migrating the PostgreSQL database in Rails

Alt tag: Terminal showing the process of creating and migrating PostgreSQL databases.

Step 6: Database Setup for PostgreSQL

To use PostgreSQL with Rails, you first need to install it On macOS: Do an installing of PostgreSQL via Homebrew.

brew install postgresql

imresizer-1725243863900
Installing PostgreSQL

Start the PostgreSQL service:

brew services start postgresql@14

Setup for PostgreSQL Database
Setup for PostgreSQL Database

Step 7: Creating a PostgreSQL Database

Log in to the PostgreSQL console and create a new database:

psql -d postgres

Creating a PostgreSQL Database
Creating a PostgreSQL Database

//sql CREATE DATABASE my_rails_app_development; CREATE DATABASE my_rails_app_test; CREATE DATABASE my_rails_app_production; CREATE USER my_app_user WITH PASSWORD 'password'; GRANT ALL PRIVILEGES ON DATABASE my_rails_app_development TO my_app_user; GRANT ALL PRIVILEGES ON DATABASE my_rails_app_test TO my_app_user; GRANT ALL PRIVILEGES ON DATABASE my_rails_app_production TO my_app_user;

Creating a PostgreSQL Database
Creating a PostgreSQL Database

Step 8: Configuring Rails to Use PostgreSQL

Next, configure your Rails software to connect to the PostgreSQL database with the aid of editing the config/database.yml document:

//yaml default: &default adapter: postgresql encoding: unicode pool: 5 username: my_app_user password: <%= ENV['PG_PASSWORD'] %> host: localhost development: <<: *default database: my_rails_app_development test: <<: *default database: my_rails_app_test production: <<: *default database: my_rails_app_production username: my_app_user password: <%= ENV['MY_APP_DATABASE_PASSWORD'] %>

Configuring Rails to Use PostgreSQL
Configuring Rails to Use PostgreSQL

Exit 'psql':

\q

Step 9: Installing the PostgreSQL Gem

Add the pg gem to your Gemfile to allow Rails to work with PostgreSQL:

Ruby
gem 'pg'
Installing the PostgreSQL Gem
Installing the PostgreSQL Gem

Run bundle install to install the gem.

bundle install

Installing the PostgreSQL Gem
Installing the PostgreSQL Gem

Step 10: Running Migrations

Create the database structure by running the following commands:

rails db:create
rails db:migrate

Creating and migrating the PostgreSQL database in Rails
Creating and migrating the PostgreSQL database in Rails

Key Terminologies

Here’s a breakdown of the key terms used in the database setup:

  1. adapter: Specifies which database engine Rails should use. For MySQL, the adapter is mysql2, and for PostgreSQL, it’s postgresql.
  2. encoding: Defines the character encoding used by the database. UTF-8 (utf8 or unicode) is commonly used to support various languages and special characters.
  3. pool: Determines the number of database connections Rails can maintain in a pool. This is useful for handling multiple simultaneous requests.
  4. username and password: These fields specify the database user's credentials that Rails will use to connect to the database. It’s a best practice to store these in environment variables.
  5. host: The host specifies the server where the database is located. Typically, it’s set to localhost for local development environments.
  6. database.yml: this is the configuration report in which Rails shops database settings for distinct environments (improvement, test, and manufacturing).
  7. package deal install: A command that installs all the gem stones indexed for your Gemfile, which include the database adapter gemstones.
  8. rails db: This command creates the databases laid out in your database.yml document.

Conclusion

In conclusion, we covered the essential steps for setting up a database in Ruby on Rails. From configuring your database to creating and managing migrations, you now have a solid foundation for integrating Active Record into your applications. Understanding relationships and best practices for database management will help ensure your app is efficient and maintainable. With this knowledge, you're well-equipped to build powerful web applications using Ruby on Rails.


Next Article
Article Tags :

Similar Reads