Setting up PostgreSQL for Rails

Juzer Shakir
Nerd For Tech
Published in
3 min readNov 16, 2021

--

A guide to newbies: to our future ROR developers!

Setting up PostgresSQL for your new project is pretty simple. Just give a few commands in the terminal and you’re done.

I still remember, for my first project of ROR I was given a task to use PostgreSQL database instead of SQLite. I spent hours trying to figure out how to set it up since I was new to rails and database technology.

As a curious developer, a question might arise in your mind, “why to use the PostgreSQL database when I have already set up SQLite?” The answer to this question is very broad and when we consider other existing database technologies such as MongoDB, MySQL, etc it gets more confusing to choose which for our application.

I won’t be answering this question here as it's beyond the scope of this article, however, here’s the link to a couple of articles that explain this thoroughly and the latter article will guide you to installing PostgreSQL in your system.

Now, let's get started.

Just to make sure you have installed the PostgreSQL in the system correctly, run the following:

psql --version

It will give an output of the PostgreSQL version you have installed in your system.

To use the PostgreSQL database in your web app, you need to create a Postgres user.

If you don’t have a user set on Postgres, here’s how to set a new user:

sudo -u postgres createuser -s username

Replace username with the username you would like to keep.

To set a password for this user, log in to the PostgreSQL command-line client:

sudo -u postgres psql

Enter the following command to set the password:

\password username

Replace username with yours and then you will be prompted to enter password twice. Enter the password you would like to keep.

Making sure user has been created, this command will give list of all users:

\du

Then exit the PostgreSQL client by:

\q

Your Postgres user has been set up. Now we need to tell rails to use this user to create Postgres database. Before that, if you haven’t set up rails application yet, follow these steps:

Make sure you use the right ruby version for creating new rails app. For this setup, I will use the 2.7.2 version which will probably generate the rails app of rails version 6.1.4.1.

$ rvm use 2.7.2$ rails new app_name --database=postgresql

The extra — database=postgresql command tells rails to use Postgres database instead of SQLite (which rails uses by default). It adds Postgres gem, gem pg, in your Gemfile. Now install all the gems in your Gemfile:

bundle install

Navigate to config/database.yml file and add this code under default: &default

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

Add the credentials of the Postgres user you want to use to create databases for all 3 environments.

In my example, I want all 3 environments (development, production & test) to use username Postgres user and other key:value pairs given under default: &default.

If you want to use different users for different environments then you need to explicitly give credentials as key:value for respective environments. Like:

development:
<<: *default
username: username
password: your_password
test:
<<: *default
username: username_2
password: your_password_2

You can also change the database name for these environments via database key.

After completing this setup, we need to tell Rails to create these Postgres databases via:

rails db:create

Voila!! That's it!

A note: you don't have to create a new Postgres user for every rails app you create, you can use an existing user by just adding its credential to database.ymlas shown.

Enjoy!

Let's connect!

--

--