Skip to content

Commit 612c897

Browse files
authored
Update baseline.md
1 parent 7a9a38c commit 612c897

1 file changed

Lines changed: 77 additions & 46 deletions

File tree

  • content/learning-paths/servers-and-cloud-computing/ruby-on-rails

content/learning-paths/servers-and-cloud-computing/ruby-on-rails/baseline.md

Lines changed: 77 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,23 @@ layout: learningpathall
77
---
88

99
## Baseline Setup for Ruby on Rails with PostgreSQL
10-
This section covers the installation and configuration of **PostgreSQL** and a **Rails application** on a SUSE Arm-based GCP VM. It includes setting up PostgreSQL, creating a Rails app, configuring the database, and starting the Rails server.
10+
This section sets up PostgreSQL and connects it with a Ruby on Rails application on a SUSE Arm64 Google Cloud C4A virtual machine. You’ll install PostgreSQL, configure it for Rails, create a database user, and verify that Rails can connect and serve requests successfully.
1111

1212
### Install and Configure PostgreSQL
13-
PostgreSQL is used with Ruby on Rails as a robust, production-ready relational database that reliably stores and manages application data.
13+
PostgreSQL is a robust, production-grade relational database that integrates seamlessly with Ruby on Rails.
14+
15+
Install PostgreSQL and its development headers:
1416

1517
```console
1618
sudo zypper install postgresql-devel postgresql-server
17-
sudo systemctl start postgresql
18-
sudo systemctl enable postgresql
1919
```
2020
- `postgresql-devel` is required to compile the pg gem for Rails.
2121

22-
Verify that the PostgreSQL service is active and running:
22+
After installation, ensure that PostgreSQL is running and configured to start automatically at boot:
2323

2424
```console
25+
sudo systemctl start postgresql
26+
sudo systemctl enable postgresql
2527
systemctl status postgresql
2628
```
2729
The output should look like:
@@ -41,55 +43,64 @@ The output should look like:
4143
├─ 27003 "postgres: autovacuum launcher " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ">
4244
└─ 27004 "postgres: logical replication launcher " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ">
4345
```
46+
If the Active state reads running, your PostgreSQL service is operational and ready for configuration.
4447

45-
This command creates a new PostgreSQL role (user) named `gcpuser` with **superuser privileges**.
48+
### Create a Database Role for Rails
49+
Next, create a dedicated PostgreSQL role (user) that Rails will use to connect to the database.
4650

4751
```console
4852
sudo -u postgres psql -c "CREATE USER gcpuser WITH SUPERUSER PASSWORD 'your_password';"
4953
```
50-
- `sudo -u postgres` → Runs the command as the `postgres` user (default PostgreSQL superuser).
51-
- `createuser --superuser gcpuser` → Creates a PostgreSQL role named `gcpuser` with full admin privileges.
52-
- Can create databases
53-
- Can create other roles/users
54-
- Can grant privileges
54+
This command:
55+
56+
Executes under the default PostgreSQL superuser account (postgres).
57+
Creates a new PostgreSQL role called gcpuser.
58+
Assigns superuser privileges, allowing the user to create databases, manage roles, and execute administrative tasks.
5559

56-
This role will be used by Rails to connect to the PostgreSQL database.
60+
This user will serve as the Rails database owner and be referenced in the Rails configuration file (config/database.yml) later.
5761

5862
### Set Environment variables
5963

60-
Before you create your Rails app, set the following environment variables:
64+
Before creating your Rails application, export environment variables so Rails and the `pg gem` can authenticate automatically with PostgreSQL.
6165

6266
```console
6367
export PGUSER=gcpuser
6468
export PGPASSWORD=your_password
6569
export PGHOST=localhost
6670
```
71+
72+
PGUSER → Specifies the PostgreSQL user that Rails will connect as.
73+
PGPASSWORD → Stores the password for that user in memory (temporary for this session).
74+
PGHOST → Points to the PostgreSQL host (in this case, the local VM).
75+
6776
### Create a Rails App with PostgreSQL
68-
Creates a new Rails application configured to use PostgreSQL as its database.
77+
Now, generate a new Rails application configured to use PostgreSQL as its default database adapter:
6978

7079
```console
7180
rails new db_test_rubyapp -d postgresql
7281
cd db_test_rubyapp
7382
bundle install
7483
```
75-
- Creates a new Rails application called `db_test_app`.
76-
- `d postgresql`Tells Rails to use PostgreSQL as the database instead of the default SQLite.
77-
- `bundle install` ensures all required gems are installed.
84+
- rails new db_test_rubyapp → Creates a new Rails application named db_test_rubyapp.
85+
- `d postgresql`Instructs Rails to use PostgreSQL instead of the default SQLite database.
86+
- bundle install → Installs all gem dependencies defined in the Gemfile, including the pg gem that connects Rails to PostgreSQL.
7887

7988
{{% notice Note %}}
8089
Check `config/database.yml` to ensure the `username` and `password` match your PostgreSQL role `(gcpuser)`.
8190
{{% /notice %}}
8291

8392
### Verify and Update Database Configuration
84-
Open and modify your Rails database configuration file:
93+
Rails uses the `config/database.yml` file to define how it connects to databases in different environments (development, test, and production).
94+
It's important to verify that these credentials align with the PostgreSQL role you created earlier.
95+
96+
Open the file with your preferred text editor:
8597

8698
```console
8799
sudo vi config/database.yml
88100
```
89-
Find the `default`: and `development`: sections.
90-
Ensure the username matches the PostgreSQL user you created (gcpuser):
101+
Locate the default and development sections, and make sure they match the PostgreSQL user and password you configured.
91102

92-
You should see output similar to:
103+
Your configuration file should have the following fields set:
93104
```output
94105
default: &default
95106
adapter: postgresql
@@ -104,34 +115,49 @@ development:
104115
```
105116

106117
### Change the Authentication Method
107-
Change the authentication method in the PostgreSQL configuration file `pg_hba.conf` from `ident` to `md5`.
118+
By default, PostgreSQL on many Linux distributions (including SUSE) uses the ident authentication method for local connections. This method maps Linux system usernames directly to PostgreSQL roles. While convenient for local access, it prevents password-based authentication, which is necessary for Rails and most application connections.
119+
120+
To allow Rails to connect using a username and password, change the authentication method in PostgreSQL’s configuration file `pg_hba.conf` from ident to md5.
108121

109122
Open your configuration file
110123
```console
111124
sudo vi /var/lib/pgsql/data/pg_hba.conf
112125
```
113-
Find lines that look like this:
126+
The file location `/var/lib/pgsql/data/pg_hba.conf` is the default data directory path for PostgreSQL on SUSE Linux.
127+
128+
Find lines like the following in the file:
129+
114130
```output
115131
# IPv4 local connections:
116132
host all all 127.0.0.1/32 ident
117133
# IPv6 local connections:
118134
host all all ::1/128 ident
119135
```
120-
Change the method on these lines to look like:
136+
Modify both lines to use md5, which enables password-based authentication:
137+
121138
```output
122139
# IPv4 local connections:
123140
host all all 127.0.0.1/32 md5
124141
# IPv6 local connections:
125142
host all all ::1/128 md5
126143
```
127-
Save the file. Restart PostgreSQL:
144+
After saving the file, restart the PostgreSQL service to apply the new authentication settings:
128145

129146
```console
130147
sudo systemctl restart postgresql
131148
```
149+
150+
Verify the change:
151+
```console
152+
sudo systemctl status postgresql
153+
```
154+
The service should show as active (running).
155+
132156
### Create and Initialize the Database
133-
Initializes and creates the development and test databases for your Rails app using PostgreSQL.
157+
Once PostgreSQL is configured and Rails can authenticate, you can create your application’s development and test databases.
158+
This step verifies that Rails is correctly connected to PostgreSQL and that the pg gem is working on your Arm64 environment.
134159

160+
Run the following command from inside your Rails app directory:
135161
```console
136162
rails db:create
137163
```
@@ -140,22 +166,26 @@ You should see output similar to:
140166
Created database 'db_test_rubyapp_development'
141167
Created database 'db_test_rubyapp_test'
142168
```
143-
This means Rails successfully connected to PostgreSQL and created both dev and test databases.
169+
This output confirms that Rails successfully. It connected to the PostgreSQL service using the credentials from `config/database.yml` and created two new databases — one for development and one for testing.
144170

145171
### Generate a Scaffold for Testing
146-
A database and Scaffold are required to create the actual PostgreSQL database for your Rails app and quickly generate the model, controller, views, and migrations for your data.
147-
Let’s create a small test model and table — for example, a simple Task tracker:
172+
To verify your Ruby on Rails and PostgreSQL integration, you’ll create a small scaffold application.
173+
A scaffold is a Rails generator that automatically builds a model, controller, views, and database migration, allowing you to test CRUD (Create, Read, Update, Delete) operations quickly.
174+
175+
For this example, you’ll create a simple Task Tracker app that manages tasks with titles and due dates.
176+
Run the following command inside your Rails project directory:
148177

149178
```console
150179
rails generate scaffold task title:string due_date:date
151180
```
152-
This command automatically generates:
153-
- Database migration for the tasks table
154-
- A model (task.rb)
155-
- A controller and views for CRUD operations
156-
- **Scaffold** → Automatically generates boilerplate code for CRUD operations, saving time and ensuring your app has working forms and routes.
181+
This single command automatically generates:
182+
A database migration to create the tasks table in PostgreSQL.
183+
A model file (app/models/task.rb) that maps to the tasks table.
184+
A controller (app/controllers/tasks_controller.rb) with full CRUD actions (index, show, new, create, edit, update, destroy).
185+
Corresponding views in app/views/tasks/ with ready-to-use HTML + embedded Ruby templates.
186+
Route entries in config/routes.rb to make the new resource accessible via /tasks.
157187

158-
Then apply the migration:
188+
Now apply the migration to create the tasks table in your PostgreSQL database:
159189

160190
```console
161191
rails db:migrate
@@ -169,17 +199,16 @@ You should see output similar to:
169199
== 20251006101717 CreateTasks: migrated (0.0128s) =============================
170200
```
171201

172-
Database schema successfully updated.
202+
This confirms that Rails connected successfully to PostgreSQL and the database schema was updated.
203+
The new tasks table was created inside your db_test_rubyapp_development database.
173204

174205
### Verify Table and Database Connectivity
175-
The previous command `rails generate scaffold task title:string due_date:date` created a `tasks` table in your PostgreSQL database.
176-
177-
Now, verify that the table exists and has the correct structure following the steps below:
206+
TThe scaffold created a tasks table in your PostgreSQL database. Verify it exists and has the expected schema:
178207

179208
```console
180209
sudo -u postgres psql
181210
```
182-
In the PostgreSQL shell, run the following commands:
211+
Inside the PostgreSQL shell, run:
183212

184213
```console
185214
\c db_test_rubyapp_development
@@ -211,7 +240,7 @@ Indexes:
211240
"tasks_pkey" PRIMARY KEY, btree (id)
212241
```
213242

214-
### Run Rails Server
243+
### Open port 3000 in Google Cloud (VPC firewall)
215244
Before proceeding to run the Rails server, you need to allow port 3000 from your GCP console. Below are the steps to do that:
216245

217246
a. On the GCP console, navigate to **Firewall** -> **Create Firewall Rule**
@@ -241,20 +270,22 @@ In the **"Protocols and Ports"**, click on **"TCP"**, and mention the port numbe
241270
Click on **"Create"**. The Firewall rule will be created successfully and can be viewed in the Firewall Policies Page:
242271

243272
![ Create Firewall rule alt-text#center](images/firewall5.png "Figure 5: Create Firewall rule")
244-
245-
Once done, go back to the VM, install FirewallD
273+
274+
### OS firewall (firewalld) on SUSE
275+
Once done, go back to your VM, install FirewallD:
246276
```console
247277
sudo zypper install firewalld
248278
```
249-
250-
Now start FirewallD and execute the below commands to allow port 3000:
279+
Now start FirewallD and execute the commands to allow port 3000:
251280

252281
```console
253282
sudo systemctl start firewalld
254283
sudo systemctl enable firewalld
255284
sudo firewall-cmd --permanent --add-port=3000/tcp
256285
sudo firewall-cmd --reload
257286
```
287+
288+
## Start Rails
258289
Now that port 3000 is allowed in your VM’s ingress firewall rules, you can start the Rails server using the following command:
259290

260291
```console
@@ -276,4 +307,4 @@ You will see a Rails welcome page in your browser if everything is set up correc
276307

277308
![Rails-info page alt-text#center](images/rails-web.png "Figure 6: Ruby/Rails Welcome Page")
278309

279-
This verifies the basic functionality of the Ruby/Rails installation before proceeding to the benchmarking.
310+
With port 3000 reachable and the welcome page loading, your Rails stack on SUSE Arm64 (C4A Axion) is verified end-to-end and you can proceed to benchmarking.

0 commit comments

Comments
 (0)