Install

Install and Configure PostgreSQL


we will use PostgreSQL as the database for our Rails project. And in this step, we will install the PostgreSQL database server provided by the official ubuntu repository.


Install the PostgreSQL database server to the Ubuntu 20.04 using the apt command below.

sudo apt install postgresql postgresql-contrib libpq-dev -y


Once all installation is completed, start the PostgreSQL service and add it to the system boot.


systemctl start postgresql

systemctl enable postgresql

The PostgreSQL service is up and running.

Next, log in to the PostgreSQL shell and create a new role 'hakase_dev' with the password 'hakasepass' and the privileges 'createdb' and 'login'.

sudo -i -u postgres psql

create role readstore with createdb login password 'Myreadstore@ezicom1';

Now list all available of users on the PostgreSQL using the following query.

\du


Start New Ruby on Rails Project with PostgreSQL Database

By default, the Ruby on Rails used the SQLite as the default database. And for this guide, we will start a new Rails project using the PostgreSQL database.

Create a new project 'hakaseapp' with the default database PostgreSQL using the following 'rails' command.

rails new readstore -d postgresql

Now you will get the project directory 'hakaseapp', go to the project directory and edit the database confoiguration 'config/database.yml' using vim editor.

cd readstore/

vim config/database.yml

On the development section, add the database configuration as below.

host: localhost

port: 5432

database: readstore_db

username: readstore

password: Readstore@ezicom1

Go to the testing section and add the PostgreSQL database configuration as below.

host: localhost

port: 5432

database: readstore_db

username: readstore

password: Readstore@ezicom1

Save and close.

Next, run the rails command below to generate and migrate the database for our Rails project, and make sure there is no error.

rails db:setup

rails db:migrate

When all setup is complete, start the default puma rails web server using the command below.

rails s -b 0.0.0.0 -p 8080

Now open your web browser and type your server IP address with port '8080' on the address bar.

http://10.142.0.4 :8080/

You will get the default index.html page of the Ruby on Rails.