Ruby on Rails Database Setup - Ruby on Rails

Let us move on with the workflow for creating Rails Application. We have already seen how to create an empty rails web application and how to start web server. Before we get into the next step, make sure your database server is up and running. It is recommended to create three databases - a database each for development, testing, and production environment which are named as follows as per convention -

  • library_development
  • library_production
  • library_test

Initialize all three of them and create a user and password for them with full read and write privileges. We are using the root user ID for our application.

Database Setup for MySQL

In MySQL, we are using the root user ID for our application. This is how the MySQL console session in which you do this looks like −

Repeat the same for two the other two databases – library_production and library_test.

Configuring database.yml

Now, you need to let Rails know about the user name and password for the databases. This is to be done in the file database.yml in the libraryconfig subdirectory of Rails Application you created. It has live configuration sections for MySQL databases. Remember to change the username and password lines in each of the sections you use, to reflect the permissions on the databases you've created .

This is how it should look once you are done -

Database Setup for PostgreSQL

By default, PostgreSQL does not provide any users. So let us create new users using the following command with the name rubyuser.

Use the following command if you want to create a password for the created new user

Use the following command for creating a database library_development.

Use the following command for creating a database library_production.

Use the following command for creating a database library_test.

Use Ctrl+D to terminate PosgreSQL.

Configuring database.yml

Configure PostgreSQL database like you configured MySQL database. Let Rails know the username and password for the databases in the file database.yml, available in the libraryconfig subdirectory of Rails Application you created. This file has live configuration sections for PostgreSQL databases. In each of the sections, you need to change the username and password lines to reflect the permissions on the databases you've created.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Ruby on Rails Topics