Creating a Ruby on Rails application with a PostgreSQL database

The ruby on rails framework uses SQLite3 as a default database. This is great for small scale projects, however, this article targets developers who want to replace SQLite3 with Postgresql, some reasons as to why you would want to replace the SQLite3 database with PostgreSQL could be for deployment scenarios such as Heroku, a Platform as a Service (PaaS) cloud environment that supports PostgreSQL and not SQLite3.


  • Ruby v.2.6.5
  • Rails v.
  • PostgreSQL v.12.2

Before reading this article, I want to be transparent about the environment that I am using:

  • WSL 2
  • Windows 10

Install Postgresql on your local machine, and ensure that it is running, the commands are listed below. However, more documentation can be found here:

Update packages:

sudo apt update

Install Postgresql:

sudo apt install postgresql postgresql-contrib

Ensure the Postgresql database is running by checking the status:

sudo service postgresql status

Starting the Postgresql database:

sudo service postgresql start

Stopping the Postgresql database:

sudo service postgresql stop

Create a new Postgres database to wire our application with. Let’s open the Postgres interactive terminal and create a database:

start the PostgreSQL shell:

sudo -u postgres psql

create the database (replace database_name with your database)

postgres=# CREATE DATABASE database_name; 

If all goes well, you should result in an output of CREATE DATABASE
if you would like you can view the database with the \l command.

Next, we should create our Rails application. Before we start let’s create a directory to store our application in:

$ mkdir directory_name
$ cd directory_name/

Once inside the directory, we can create our new application, specifying that we want Postgres as our database:

$ rails new app_name -d=postgresql

once the app is generated, we’re not quite finished yet, we have to configure our application settings to use the appropriate Postgres database that we created. Navigate to config/database.yml file, and ensure that the database name is correct:

  <<: *default
  database: database_name

Once that is completed save the file and run the server. Navigate to http://localhost:3000/ and you should see that you have a Ruby on Rails application running with a PostgreSQL database.


If you happen to run into a user role error, create a role within the application that matches the error message or the correct postgresql user to the database.yml file:

Creating a user in Postgresql shell:

postgres=# CREATE ROLE role_name; 

List the users within the database: \du

documentation on PostgreSQL can be found here:

Congratulations! for more information, please visit my blog section.