Home Ruby on Rails Configure Database Connection Using Environment Variable In Rails

Configure Database Connection Using Environment Variable In Rails

By sk
4.3K views

This guide explains why you need to use an environment variable to connect to a database and how to configure database connection using environment variable in Rails application in Linux.

Why use environment variable to configure database?

Every Rails application will interact with its respective database. There are two ways to connect with a database in a Rails application. You can connect to a database via a database configuration file called config/database.yml or using an environment variable called ENV['DATABASE_URL']. We already have shown you how to create a Rails application with a MySQL database or a PostgreSQL database. In these two guides, we configured the database connection using config/database.yml file.

If you're the only one person working in a Rails project, it is OK to store database credentials in the config/database.yml file. But if the project is hosted in a public Git repository and many developers are working on it, it is not recommended to store sensitive information like passwords in the config/database.yml file. Also, every developer's database details can be different i.e. username and password. Every time we push the code, there will be a conflict in a file. This is why we need to store the database credentials in an environment variable rather than to write it directly in the database configuration file.

Configure database connection using environment variable in Rails application

I assume that you have latest Ruby on Rails in your Linux machine. If you haven't installed Ruby on Rails yet, refer the following guide.

Next, create a new Rails application. If want to use MySQL with Rails application, run the following command to create a new Rails application namely ostechnixapp:

$ rails new ostechnixapp -d mysql

If you want to use PostgreSQL with Rails application, create the new Rails application called ostechnixapp like below:

$ rails new ostechnixapp -d postgresql

Next, add new environment variable with your database user password in your user's profile file, for example ~/.bashrc.

$ echo 'export OSTECHNIXAPP_DATABASE_PASSWORD="Database_User_Password"' >> ~/.bashrc

In the above command, replace "OSTECHNIXAPP" with your application name and "Database_User_Password" with your actual database user's password.

Source the ~/.bashrc file to update the changes immediately:

$ source ~/.bashrc
Add environment variable to bashrc file
Add environment variable to bashrc file

Now switch to the application directory:

$ cd ostechnixapp/

Edit the application's database configuration file:

$ vi config/database.yml

If you're using MySQL, add the database user name and the environment variable which we added to the ~/.bashrc file in the previous step, under the default section. Here is what I've added in MySQL configuration file:

[...]
default: &default
   adapter: mysql2
   encoding: utf8mb4
   pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
   username: root
   password: <%= ENV['OSTECHNIXAPP_DATABASE_PASSWORD'] %>
   socket: /var/run/mysqld/mysqld.sock
[...]
Configure MySQL Database Connection Using Environment Variable
Configure MySQL Database Connection Using Environment Variable

Again, you must replace OSTECHNIXAPP_DATABASE_PASSWORD with your correct environment variable.

If you're using PostgreSQL, add the database role and environment variable under default section like below:

[...]
default: &default
   adapter: postgresql
   encoding: unicode
   # For details on connection pooling, see Rails configuration guide
   # https://guides.rubyonrails.org/configuring.html#database-pooling
   pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
   username: ostechnix
   password: <%= ENV['OSTECHNIXAPP_DATABASE_PASSWORD'] %>
[...]
Configure PostgreSQL Database Connection Using Environment Variable
Configure PostgreSQL Database Connection Using Environment Variable

After adding the database user role and the environment variable, press ESC key and type :wq to save the changes and exit the file.

Finally, create the database for your Rails application using command:

$ rake db:create

This command will create two databases namely ostechnixapp_development and ostechnixapp_test.

Created database 'ostechnixapp_development'
Created database 'ostechnixapp_test'

Now start the Rails application on the built-in web server called puma using command:

$ rails server -b 0.0.0.0

Open your web browser and access the Rails application by navigating to http://ip-address:3000 URL. You will be greeted with Rails default test page:

Ruby on Rails default test page
Ruby on Rails default test page

Like I already, if you don't want to disclose the database password to other developers, you should configure the database connection with an environment variable in a Rails application. If you're sole developer, then you can simply use the default database configuration file.

Resource:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More