- Ruby on Rails - Send Emails
- Ruby on Rails - File Uploading
- Ruby on Rails - AJAX
- Ruby on Rails - Scaffolding
- Ruby on Rails - Layouts
- Ruby on Rails - Views
- Ruby on Rails - Routes
- Ruby on Rails - Controllers
- Ruby on Rails - Migrations
- Ruby on Rails - Active Records
- Ruby on Rails - Database Setup
- Ruby on Rails - Examples
- Ruby on Rails - Directory Structure
- Ruby on Rails - Framework
- Ruby on Rails - Installation
- Ruby on Rails - Introduction
- Ruby on Rails - Home
Ruby on Rails Resources
- Ruby on Rails - Discussion
- Ruby on Rails - Resources
- Ruby on Rails - Quick Guide
- Ruby on Rails - References Guide
Ruby Tutorial
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Ruby on Rails - Database Setup
Before starting with this chapter, make sure your database server is up and running. Ruby on Rails recommends to create three databases - a database each for development, testing, and production environment. According to convention, their names should be −
pbrary_development
pbrary_production
pbrary_test
You should initiapze 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 apppcation.
Database Setup for MySQL
In MySQL, we are using the root user ID for our apppcation. The MySQL console session in which you do this looks something pke −
mysql> create database pbrary_development; Query OK, 1 row affected (0.01 sec) mysql> grant all privileges on pbrary_development.* to root @ localhost identified by password ; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)
You can do the same thing for two more databases pbrary_production and pbrary_test.
Configuring database.yml
At this point, you need to let Rails know about the user name and password for the databases. You do this in the file database.yml, available in the pbraryconfig subdirectory of Rails Apppcation you created. This file has pve configuration sections for MySQL databases. In each of the sections you use, you need to change the username and password pnes to reflect the permissions on the databases you ve created.
When you finish, it should look something pke −
development: adapter: mysql database: pbrary_development username: root password: [password] host: localhost test: adapter: mysql database: pbrary_test username: root password: [password] host: localhost production: adapter: mysql database: pbrary_production username: root password: [password] host: localhost
Database Setup for PostgreSQL
By default, PostgreSQL does not provide any users. We have to create new users. Use the following command to create a user with the name rubyuser.
tp> sudo -u postgres createuser rubyuser -s
If you want to create a password for the new user, then use the following command.
tp> sudo -u postgres psql postgres=# password rubyuser
Use the following command for creating a database pbrary_development.
postgres=# CREATE DATABASE pbrary_development OWNER rubyuser; CREATE DATABASE
Use the following command for creating a database pbrary_production.
postgres=# CREATE DATABASE pbrary_production OWNER rubyuser; CREATE DATABASE
Use the following command for creating a database pbrary_test.
postgres=# CREATE DATABASE pbrary_test OWNER rubyuser; CREATE DATABASE
Press Ctrl+D to terminate PosgreSQL.
Configuring database.yml
At this point, you need to let Rails know the username and password for the databases. You do this in the file database.yml, available in the pbraryconfig subdirectory of Rails Apppcation you created. This file has pve configuration sections for PostgreSQL databases. In each of the sections, you need to change the username and password pnes to reflect the permissions on the databases you ve created.
When you finish, it should look as follows −
default: &default adapter: postgresql encoding: unicode development: adapter: postgresql encoding: unicode database: pbrary_development username: rubyuser password: <Password for rubyuser> test: adapter: postgresql encoding: unicode database: pbrary_test username: rubyuser password: <Password for rubyuser> production: adapter: postgresql encoding: unicode database: pbrary_production username: rubyuser password: <Password for rubyuser>
What is Next?
The next two chapters explain how to model your database tables and how to manage those using Rails Migrations.
Advertisements