Configure Database Connection Utilizing Atmosphere Variable In Rails


Get real time updates directly on you device, subscribe now.

This information explains why it is advisable use an setting variable to connect with a database and the right way to configure database connection utilizing setting variable in Rails utility in Linux.

Why use setting variable to configure database?

Each Rails utility will work together with its respective database. There are two methods to attach with a database in a Rails utility. You may connect with a database by way of a database configuration file known as config/database.yml or utilizing an setting variable known as ENV[‘DATABASE_URL’]. We have already got proven you the right way to create a Rails utility with a MySQL database or a PostgreSQL database. In these two guides, we configured the database connection utilizing config/database.yml file.

In the event you’re the one one individual working in a Rails challenge, it’s OK to retailer database credentials within the config/database.yml file. But when the challenge is hosted in a public Git repository and plenty of builders are engaged on it, it’s not really helpful to retailer delicate data like passwords within the config/database.yml file. Additionally, each developer’s database particulars might be completely different i.e. username and password. Each time we push the code, there will likely be a battle in a file. For this reason we have to retailer the database credentials in an setting variable moderately than to put in writing it instantly within the database configuration file.

Configure database connection utilizing setting variable in Rails utility

I assume that you’ve newest Ruby on Rails in your Linux machine. If you have not put in Ruby on Rails but, refer the next information.

Subsequent, create a brand new Rails utility. If wish to use MySQL with Rails utility, run the next command to create a brand new Rails utility particularly ostechnixapp:

$ rails new ostechnixapp -d mysql

If you wish to use PostgreSQL with Rails utility, create the brand new Rails utility known as ostechnixapp like beneath:

$ rails new ostechnixapp -d postgresql

Subsequent, add new setting variable together with your database consumer password in your consumer’s profile file, for instance ~/.bashrc.

$ echo ‘export OSTECHNIXAPP_DATABASE_PASSWORD=”Database_User_Password”‘ >> ~/.bashrc

Within the above command, change “OSTECHNIXAPP” together with your utility title and “Database_User_Password” together with your precise database consumer’s password.

Supply the ~/.bashrc file to replace the adjustments instantly:

$ supply ~/.bashrcAdd environment variable to bashrc fileAdd setting variable to bashrc file

Now change to the appliance listing:

$ cd ostechnixapp/

Edit the appliance’s database configuration file:

$ vi config/database.yml

In the event you’re utilizing MySQL, add the database consumer title and the setting variable which we added to the ~/.bashrc file within the earlier step, underneath the default part. Here’s what I’ve added in MySQL configuration file:

default: &default
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root

socket: /var/run/mysqld/mysqld.sock
[…]Configure MySQL Database Connection Using Environment VariableConfigure MySQL Database Connection Utilizing Atmosphere Variable

Once more, you could change OSTECHNIXAPP_DATABASE_PASSWORD together with your right setting variable.

In the event you’re utilizing PostgreSQL, add the database function and setting variable underneath default part like beneath:

default: &default
adapter: postgresql
encoding: unicode
# For particulars on connection pooling, see Rails configuration information
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: ostechnix

[…]Configure PostgreSQL Database Connection Using Environment VariableConfigure PostgreSQL Database Connection Utilizing Atmosphere Variable

After including the database consumer function and the setting variable, press ESC key and sort :wq to avoid wasting the adjustments and exit the file.

Lastly, create the database in your Rails utility utilizing command:

$ rake db:create

This command will create two databases particularly ostechnixapp_development and ostechnixapp_test.

Created database ‘ostechnixapp_development’
Created database ‘ostechnixapp_test’

Now begin the Rails utility on the built-in internet server known as puma utilizing command:

$ rails server -b

Open your internet browser and entry the Rails utility by navigating to http://ip-address:3000 URL. You may be greeted with Rails default take a look at web page:

Ruby on Rails default test pageRuby on Rails default take a look at web page

Like I already, in case you do not wish to disclose the database password to different builders, it is best to configure the database reference to an setting variable in a Rails utility. In the event you’re sole developer, then you possibly can merely use the default database configuration file.

Useful resource:

Leave A Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More