Support Online
Skip to main content

PostgreSQL Installation and Configuration with Ruby on Rails

📘 What Will You Learn in This Guide?

In this guide, you will learn how to install, configure and test your Ruby on Rails application with a PostgreSQL database. Rails installation, PostgreSQL user creation, database.yml configuration and database testing are explained in a simple and understandable manner.


🧠 Technical Summary

The purpose of this guide is to ensure that the Ruby on Rails (RoR) framework and the PostgreSQL database work together. Steps; It covers the entire process, starting from Ruby and Rails installation, installing PostgreSQL, adding the connection gem, user creation, project configuration and database testing.


⚙️ Step by Step Installation and Configuration

You can easily follow these steps on your local machine or cloud server (e.g. tr1-node01.ornek.com).


1. Ruby and Rails Installation

The easiest way to install Rails is to use RVM (Ruby Version Manager). RVM lets you manage different Ruby versions and automatically installs the Rails gem.

\curl -L https://get.rvm.io | bash -s stable --rails

📝 This command installs Ruby and Rails on your system.

Alternatively, if you have existing RVM you can just install the Rails gem:

gem install rails

📝 This command installs Rails and dependent gems.


2. PostgreSQL and pg Gem Installation

PostgreSQL installation may vary depending on your operating system. Ubuntu example:

sudo apt-get install postgresql postgresql-contrib

📝 This command installs the PostgreSQL database server.

To establish the connection between Ruby and PostgreSQL, install the pg gem:

gem install pg

📝 This gem allows Ruby code to talk to the Postgres database.


3. Creating a PostgreSQL User (Role)

Create a custom database user for the Rails application.

su - postgres
psql
create role genixnode_app with createdb login password 'gucluvegizlisifre';
\q

📝 This action creates a new database user named genixnode_app.


4. Creating a New Rails Application

Specify the Postgres database when starting a new Rails application:

rails new genixnode-proje --database=postgresql
cd genixnode-proje

📝 This command creates a Rails application with Postgres configuration.


5. Configuring Database Settings

Rails connects to Postgres using the information in the config/database.yml file. Edit the example below with your own user information:

development:
adapter: postgresql
encoding: unicode
database: genixnode_proje_development
pool: 5
username: genixnode_app
password: gucluvegizlisifre

test:
adapter: postgresql
encoding: unicode
database: genixnode_proje_test
pool: 5
username: genixnode_app
password: gucluvegizlisifre

6. Starting and Moving Databases

You can now create and test databases:

rake db:setup

📝 This command creates the development and test databases.

Let's create a simple model for testing:

rails g scaffold Yazi baslik:string icerik:text
rake db:migrate

📝 These commands create the Yazi model and apply it to the database.


7. Running the Application

To start your Rails app:

rails server

Test the connection by going to http://localhost:3000/yazis from the browser. If everything is configured correctly, the PostgreSQL database is running successfully.


❓ Frequently Asked Questions (FAQ)

1. Can I use MySQL instead of PostgreSQL?

Yes, Rails also supports different databases like MySQL and SQLite.

2. I am getting an error when installing the pg gem, why?

PostgreSQL development libraries may be missing. For Ubuntu:

sudo apt-get install libpq-dev

3. What does pool: 5 in database.yml mean?

This specifies the maximum number of database connections that can be opened simultaneously.

4. Postgres settings do not appear in the rails new command, why?

If you forgot to add the --database=postgresql parameter, you must edit the file manually.

5. What should I pay attention to when using a cloud server?

Open port 3000 on the firewall and use strong passwords in the production environment.


🎯 Result

Now your Ruby on Rails application is fully integrated with the PostgreSQL database. If all connections are configured correctly, model building and queries will run smoothly.

💡 On the GenixNode platform, you can immediately try your Ruby on Rails applications on high-performance Postgres servers.