In previous blogs, we have seen the installation of Ruby on Rails with Sqlite3. In this blog, we are going to cover following topics:

  • Installation of Postgresql with Rails database set up
  • Rails app for Post and Comments CRUD operations.

Step 1 – Setup PostgreSQL Database for Rails Development

By default, Ruby on Rails is using the SQLite database. It supports many databases system, including MySQL, SQLite, and PostgreSQL. And for this guide, we will be using PostgreSQL.

Install the PostgreSQL database using the apt command below.

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

After all installation is complete, start the Postgres service and enable it to launch everytime at system boot.

systemctl start postgresql

systemctl enable postgresql

Next, we will configure a password for the Postgres user, and create a new user for the Rails installation.

Login to the ‘postgres’ user and run the Postgres shell.

su – postgres

psql

Change the Postgres password using the query below.

\password postgres

Type your password and the password for postgres user has been added.

Now we will create a new role for our rails installation. We will create a new role named ‘rails_dev’ with the privilege of creating the database and with the password ‘aqwe123’.

Run the Postgres query below.

create role rails_dev with createdb login password ‘aqwe123’;

Now check all available roles on the system.

\du

And you will get the ‘rails_dev’ role on the list.

PostgreSQL installation and configuration for Rails Development has been completed.

Step 2 – Create Your First App with Rails and PostgreSQL

Ruby on Rails provides a command-line ‘rails’ for bootstrapping our first rails application.

Create a new project ‘myapp’ with default database ‘PostgreSQL’ by running rails command below.

rails new myapp -d postgresql

Now you will see the ‘myapp’ directory, go to that directory and edit the database configuration file ‘database.yml’ using vim editor.

cd myapp/

vim config/database.yml

There are different configuration sections for each setup – Development, Testing, and Production.

In the development section, uncomment those line and change the value as below.

username: rails_dev

password: aqwe123

host: localhost

port 5423

For the testing section, paste those configurations under the testing section.

host: localhost

port: 5432

  username: rails_dev

  password: aqwe123

Save and exit.

Now generate the database 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 192.168.1.10 -p 8080

The first rails project will be running on the IP address ‘192.168.1.10’ with port 8080.

Open your web browser and type the server IP address on the address bar.

http://192.168.1.10:8080/

You will get the default rails project homepage as below.

Next, we will test to create simple CRUD with PostgreSQL database on rails.

Run the rails command below.

rails g scaffold Post title:string body:text

rake db:migrate

Run the puma web server again.

rails s -b 192.168.1.10 -p 8080

And open the web browser with the URL below.

http://192.168.1.10:8080/posts/

Now you will get the simple CRUD form.

And following is my result after creating a simple post.

Ruby on Rails installation with PostgreSQL database on Ubuntu 18.04 LTS has been completed successfully.