Support Online
Skip to main content

Using Ruby on Rails with PostgreSQL (macOS – The Ultimate Guide)

🧠 Technical Summary

In this guide, you'll learn how to use a Ruby on Rails development environment on macOS with a PostgreSQL database instead of the default SQLite. The goal is to achieve PostgreSQL integration to provide higher data integrity and flexibility in Rails applications.

Steps:

  1. PostgreSQL installation
  2. Creating a database role (user)
  3. Starting a Rails project
  4. Database configuration and environment variable setting
  5. Testing the configuration

💡 What You Will Learn in This Guide

  • Installing PostgreSQL using Homebrew
  • Creating roles and passwords in PostgreSQL
  • Configuring Rails application with PostgreSQL
  • Ensuring secure connection with environment variables
  • Verifying the connection by testing the Rails server

⚙️ 1. Installing PostgreSQL Database

PostgreSQL can be easily installed via Homebrew.

brew install postgresql

This command installs the latest version of PostgreSQL and the necessary dependencies.

Add PostgreSQL commands to the PATH variable (example version: 10):

echo 'export PATH="/usr/local/opt/postgresql@10/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile

Makes PostgreSQL accessible in the terminal.

Start the service and make it run automatically at system startup:

brew services start postgresql@10
postgres -V

Checks your PostgreSQL version and starts the service.


🧩 2. Creating a Database Role for Rails Application

Create a custom PostgreSQL role that your Rails application will use:

createuser -P -d appuser

Creates a role named appuser and grants permission to create a database.

If you want to change the password later, enter the PostgreSQL console:

psql postgres
\password appuser
\q

With these commands, you can set a new password and exit the console.


🧱 3. Starting a New Rails Project

Create a new project to use PostgreSQL:

rails new blogapp -d=postgresql
cd blogapp

The -d=postgresql option enables your Rails project to use PostgreSQL.


⚙️ 4. Database Configuration

Create an environment variable to store the password securely:

echo 'export BLOGAPP_DATABASE_PASSWORD="gizli_sifre"' >> ~/.bash_profile
source ~/.bash_profile

Thanks to the environment variable, the password is stored outside the code.

Edit the config/database.yml file:

default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") &#123; 5 &#125; %>
username: appuser
password: <%= ENV['BLOGAPP_DATABASE_PASSWORD'] %>

development:
<<: *default
database: blogapp_development

These settings ensure that Rails connects to PostgreSQL with the correct user information.

Create the databases:

rails db:create

This command creates development and test databases.


🧪 5. Testing the Configuration

Start the Rails server:

rails server --binding=127.0.0.1

The application will run on port 3000 by default.

To test the connection:

curl http://127.0.0.1:3000

If the Rails welcome page appears, the PostgreSQL connection has been established successfully.

Check that your app is working by going to http://127.0.0.1:3000 from the browser.


❓ Frequently Asked Questions (FAQ)

1. Why should I use PostgreSQL instead of SQLite?

PostgreSQL is much more powerful in terms of complex queries, high scalability and data integrity.

2. Do I have to use Homebrew on macOS?

No, you can also install PostgreSQL directly from the official website or via Postgres.app.

3. What exactly is a PostgreSQL role?

A role represents users in PostgreSQL and defines database access permissions.

4. Why use environment variables?

It increases security by preventing sensitive information such as passwords from being written directly in the file.

5. Which versions are Rails compatible with PostgreSQL?

Rails generally works fine with all versions of PostgreSQL 9.6 and above.


🎯 Result

You have now set up a Ruby on Rails development environment with PostgreSQL support on macOS. You've configured your database, created your user role, and your application has run successfully. 💡 You can host your projects on GenixNode with PostgreSQL support and benefit from the scalable infrastructure.