CodeX
Published in

CodeX

Why Do I Use PostgreSQL For My Rails Backend & How To Set It Up

As Full Stack Software Engineers, one of our tasks during a coding project is to build a backend. The backend is the server-side development of our projects. It’s where we focus on scripting, website architecture, and this article’s topic, the database. A database is a systematic collection of data. It’s where we’re going to store and manipulate it to present it to the end-users in the front end. In other words, the backend it’s where we’re going to code the relationship between the front-end presentation and the database.

The backend (server-side) framework application I learned is no other than Ruby on Rails. I feel lucky to come across a Bootcamp that teaches Rails. First, Ruby is an easy code to understand, second the use of ActiveRecord, a pattern that describes the Object Relational Mapping (ORM) technique that connects objects to an application to tables in a relational database management system (RDBMS). That’s how the MVC (see your Rails project repo, and you’ll get that it means Model, View, Controller) architecture works.

For data objects to connect to an RDBMS, we need the correct database system that will make the best out of our ActiveRecord patterned backend. There are always three SQL (NoSQL databases would be a topic for later, once I try it) brought in the Rails conversation; SQLite, a built-in with Rails and recommended for small projects. MySQL, another DBMS and used by many web developers due to its performance on web applications. And PostgreSQL, which is ODBMS, is a modified object-oriented version of DBMS.

My preferred database is PostgreSQL, and here are the five reasons for choosing it.

Documentation

With the rise in popularity, there’s also an incremented amount of documentation. Though MySQL is more used for historical reasons, more developers and big companies, choosing PostgreSQL. Along with their documentation, there’s also a lot of contributors for the open-source ORDBMS, and you can check the source code on Github.

Extensibility

Speaking about open-source, PostgreSQL is an extensible database. By simple definition, it will accept any data type not available in MySQL (geometric/GIS, network address types, JSONB, native UUID, timezone-aware timestamps). That’s pretty good, and the most exciting part is that we can also add our datatypes, operators, and index types.

The technical definition of extensibility is a catalog-driven operation that includes more information than just the tables and also has information about data types, functions, access methods, etc. You can check out the entire explanation in their documentation.

Data Integrity

PostgreSQL it’s fully ACID (Atomicity, Consistency, Isolation, Durability) compliant and is well-known for its referential and transactional integrity. When it comes to Rails and ActiveRecord, this is a great combination, considering how relatively simple data validation in Rails can be.

create_table :users do |t|
t.string :name, index: true
t.string :email, index: { unique: true, name: 'unique_emails' }
end

In the above migration code taken from the rails documentation, we’re requesting the database to create the table :users with the columns :name with an index included and the column :email that will be unique and will be called unique_emails. This code is an example of comments that can be added to the database.

class User < ApplicationRecord
validates :name, presence: true
validate :email, uniqueness: true
end

Also, keep in mind, we can also validate data in the model. PostgreSQL works better with data integrity, is a good option for complex data structures.

Recommended for Heroku

Eventually, we want to upload our projects. The most popular cloud platform to publish or upload those successful projects that make us feel accomplished and proud is Heroku. The SQL database supported by Heroku is PostgreSQL. Here’s their documentation with more information about it.

Prepare For Bigger Project

There are other reasons for implementing PostgreSQL. Its low reading speed is a disadvantage. But let’s take a look at the data, it’s popular, and I’d rather have exposure to something I believe I’ll come across in the professional. This reason is more like a corny self-help thing but, popularity does play a huge in my decision.

How to Implement in a new project:

Now that I explained why I use it, you can also try it. Here are the instructions on how to set it up.

Install PostgreSQL

Please refer to the PostgreSQL download page and follow the instructions according to your operating system. Whether your command will be brew, sudo, zypper, yum, or any other, there’s a download package with its instructions for everyone. Once you complete your installation, it’s time to move on to starting your new Rails app.

Check your rails version

Now that Rails 7.0.0, and I’m excited about it, I’d like to remind you to check the latest Rails version and install the new update if required.

rails -vgem update rails

Create the new API with PostgreSQL

rails new api-with-postgresql --api --database=postgresql

Enter the above command, please be aware that - api will make rails install the new app with less middleware than a full-stack rails app. — database=postgresql is for connecting the new app with your PostgreSQL. Forgetting that portion of the command will make the new Rails app use the built-in SQLite.

Check Your Gemfile

Check your gemfile and make sure you have the following gem.

gem ‘pg’

In case you don’t have it, please add it and enter bundle install in the terminal.

How do I know I’m using it?

Good question, go to file config/database.yml and you should see something similar as this:

default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

There we have it, hope this inspires you to try PostgreSQL but most importantly, create a new project and keep on practicing and learning something new every day.

Happy coding!

Summary:

  1. Introduction
  2. What is an RDBMS
  3. Reasons for choosing PostgreSQL
  4. How to implement PostgreSQL in a new project

References:

  1. Guru99 — Introduction to Database, What is a Backend Developer
  2. Ruby on Rails Official Documentation
  3. PostgreSQL Website
  4. Heroku — Heroku PostgreSQL
  5. Stackshare

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store