Ruby on Rails with PostgreSQL featured image

Setting up Ruby on Rails with PostgreSQL

Among all the available database engines, PostgreSQL is one of the most popular ones. It’s a well-known open-source database system known for its reliability, robust features, and performance. Because PostgreSQL is a popular choice, it’s supported by almost all major web frameworks. Similar to its competitors MySQL and SQLite, PostgreSQL also has its strong and weak points.

Ruby on Rails is a popular web framework written in Ruby. It’s a database-agnostic framework, meaning it can work with a wide variety of databases. By default, Rails assume SQL for database functionalities. However, it also supports PostgreSQL integration.

This tutorial will guide you through the process of configuring PostgreSQL for your Ruby on Rails application.

Ruby on Rails and PostgreSQL

Both PostgreSQL and Ruby on Rails are available on any UNIX/Linux distribution. They should be available from the default package servers.

An alternative method is using RVM (Ruby Version Manager). It’s a dedicated tool to manage Ruby on Rails installations. The benefit of this approach is that RVM will create a local installation of Ruby on Rails. It can also manage multiple versions of Ruby and switch seamlessly between them. Here is a tutorial that details the steps of Installing Ruby on Rails with RVM on Ubuntu.

As for PostgreSQL, take a look at this quick guide on installing and configuring PostgreSQL on Ubuntu.

  • Installing RVM

The installation process of RVM is pretty straightforward, thanks to the installation script RVM offers. The script is designed to auto-detect the Linux system, then download and install all necessary packages. We need the RVM GPG keys to verify packages. It ensures that the packages received aren’t spoofed. First, add the RVM GPG keys:

gpg_recv_keys Ruby on Rails with PostgreSQL

Then, run the RVM installation script:

install_rvm Ruby on Rails with PostgreSQL

Finally, verify the installation:

  • Installing Rails

We can now use RVM to install additional components. Install the Rails framework using RVM:

gem_install_rails Ruby on Rails with PostgreSQL
  • Installing PostgreSQL

The next step requires having PostgreSQL pre-installed on the system. By default, the easiest way to install PostgreSQL is using the default package manager. It’s available for all the major Linux distros. Check out the PostgreSQL download page for all the available installation methods. On Ubuntu, the following commands will configure the PostgreSQL repo, and install it right away:



apt_and_postgresql_update Ruby on Rails with PostgreSQL

To enable PostgreSQL support, we need to install the pg gem. It enables interfacing with PostgreSQL from Ruby code:

  • Configuring PostgreSQL

After the installation, we will configure the tools we will need to use for our projects. First, switch to the PostgreSQL user:

Access the PostgreSQL shell:

psql Ruby on Rails with PostgreSQL

In the context of PostgreSQL, we will now create a new role (classically known as user). PostgreSQL has its own format of roles and permissions. Don’t forget to use an appropriate username and a strong password:


Creating a Rails App

Tell Rails to create a new Rails app configured to use PostgreSQL for the database:


This will create a new directory with the app username. Rails generally expect the username of the database and the app name to be the same. The next step is to configure the Rails database configuration. The configuration file is located at the following location:

The file database.yml contains the data describing the appropriate database for the current Rails environment. As the filename suggests, it uses YAML. YAML is a human-friendly data serialization standard for all programming languages.

By default, Rails expects different databases for different environments, for example, development, test, and production. It’s useful in various situations, for example, Rails will empty and rebuild the test database every time a Rails test is run.

Here’s a sample database.yml that contains the development and test databases:


It is now time to take the changes into effect. The following command will prepare the necessary databases described in database.yml. Each database will have its own users and have schema_migrations tables. The table is necessary for data and schema migration:

This step will fail if the PostgreSQL configuration file pg_hba.conf contains improper configurations. The file is located at the following location:

The following line is the target. Instead of peer, the authentication method should be md5:

Change the line:


To take the changes into effect, you have to restart the PostgreSQL service:

Then, re-run the database setup:


Running Rails

Running the Rails server requires the following additional component. Note that for this, you need Node.js installed (with yarn):


The Rails app should now be ready to launch. Start the Rails server:


The server should be accessible from the following URL. It will land on the Rails landing page:

To make the landing page more exciting, create a scaffold:



Finally, start the server and navigate to the URL again. We can now manage posts (creating, editing, and deleting posts).

Final Thoughts

This is a basic setup of Ruby on Rails with PostgreSQL. There’s an official getting started guide that will help you further utilize Rails. The sample app we created uses PostgreSQL. This way, you can create any app that uses PostgreSQL for its functionalities.

Happy computing!