You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/learning-paths/servers-and-cloud-computing/ruby-on-rails/baseline.md
+77-46Lines changed: 77 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,21 +7,23 @@ layout: learningpathall
7
7
---
8
8
9
9
## 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.
11
11
12
12
### 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.
If the Active state reads running, your PostgreSQL service is operational and ready for configuration.
44
47
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.
46
50
47
51
```console
48
52
sudo -u postgres psql -c "CREATE USER gcpuser WITH SUPERUSER PASSWORD 'your_password';"
49
53
```
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.
55
59
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.
57
61
58
62
### Set Environment variables
59
63
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.
61
65
62
66
```console
63
67
export PGUSER=gcpuser
64
68
export PGPASSWORD=your_password
65
69
export PGHOST=localhost
66
70
```
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
+
67
76
### 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:
69
78
70
79
```console
71
80
rails new db_test_rubyapp -d postgresql
72
81
cd db_test_rubyapp
73
82
bundle install
74
83
```
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.
78
87
79
88
{{% notice Note %}}
80
89
Check `config/database.yml` to ensure the `username` and `password` match your PostgreSQL role `(gcpuser)`.
81
90
{{% /notice %}}
82
91
83
92
### 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:
85
97
86
98
```console
87
99
sudo vi config/database.yml
88
100
```
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.
91
102
92
-
You should see output similar to:
103
+
Your configuration file should have the following fields set:
93
104
```output
94
105
default: &default
95
106
adapter: postgresql
@@ -104,34 +115,49 @@ development:
104
115
```
105
116
106
117
### 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.
108
121
109
122
Open your configuration file
110
123
```console
111
124
sudo vi /var/lib/pgsql/data/pg_hba.conf
112
125
```
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
+
114
130
```output
115
131
# IPv4 local connections:
116
132
host all all 127.0.0.1/32 ident
117
133
# IPv6 local connections:
118
134
host all all ::1/128 ident
119
135
```
120
-
Change the method on these lines to look like:
136
+
Modify both lines to use md5, which enables password-based authentication:
137
+
121
138
```output
122
139
# IPv4 local connections:
123
140
host all all 127.0.0.1/32 md5
124
141
# IPv6 local connections:
125
142
host all all ::1/128 md5
126
143
```
127
-
Save the file. Restart PostgreSQL:
144
+
After saving the file, restart the PostgreSQL service to apply the new authentication settings:
128
145
129
146
```console
130
147
sudo systemctl restart postgresql
131
148
```
149
+
150
+
Verify the change:
151
+
```console
152
+
sudo systemctl status postgresql
153
+
```
154
+
The service should show as active (running).
155
+
132
156
### 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.
134
159
160
+
Run the following command from inside your Rails app directory:
135
161
```console
136
162
rails db:create
137
163
```
@@ -140,22 +166,26 @@ You should see output similar to:
140
166
Created database 'db_test_rubyapp_development'
141
167
Created database 'db_test_rubyapp_test'
142
168
```
143
-
This means Rails successfullyconnected 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.
144
170
145
171
### 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:
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