简体   繁体   中英

How to use environment variable to avoid hard-coding postgresql's username and password in database.yml?

I created a new Rails app called sample_app and I use postgresql as my db (I already created a postgresql username and password). I use this setup guide https://gorails.com/setup/ubuntu/16.04

So I run this command rails new sample_app -d postgresql . And then I have to edit the config/database.yml to match the username and password to my postgresql's username and password I just created. But I don't want to hard-code because I will be using git.

I found this tutorial from digital ocean which suggest to use:

username: <%= ENV['APPNAME_DATABASE_USER'] %>
password: <%= ENV['APPNAME_DATABASE_PASSWORD'] %>

Is this the correct code? If so, since my app is called sample_app, my code should be?

username: <%= ENV['SAMPLE_APP_DATABASE_USER'] %>
password: <%= ENV['SAMPLE_APP_DATABASE_PASSWORD'] %>

If this is not the correct one, can you help me? Thank you!

There are many ways you can set the environment variables.

Here are two of them,

Option One: Setting ENV variables via a yml file

Create a file config/local_env.yml :

config/local_env.yml:

SAMPLE_APP_DATABASE_USER: 'your username'
SAMPLE_APP_DATABASE_PASSWORD: '******'

The above are the names you will use like, ENV['SAMPLE_APP_DATABASE_USER'] . these can be names as your wish . you can take any name, but we should use the same name in the ENV reference .

add it to gitignore:

/config/local_env.yml

Change some code in application.rb

application.rb:

config.before_configuration do
  env_file = File.join(Rails.root, 'config', 'local_env.yml')
  YAML.load(File.open(env_file)).each do |key, value|
    ENV[key.to_s] = value
  end if File.exists?(env_file)
end

The code opens the config/local_env.yml file, reads each key/value pair, and sets environment variables.

Using Environment Variables:

username: <%= ENV['SAMPLE_APP_DATABASE_USER'] %>
password: <%= ENV['SAMPLE_APP_DATABASE_PASSWORD'] %>

Option Two: Use the Figaro Gem

The gem takes advantage of Ruby's ability to set environment variables as well as read them. The gem reads a config/application.yml file and sets environment variables before anything else is configured in the Rails application.

Here's how to use it. In your Gemfile, add:

gem 'figaro'

and run bundle install

The gem provides a generator:

$ bundle exec figaro install

The generator creates a config/application.yml file and modifies the .gitignore file to prevent the file from being checked into a git repository.

You can add environment variables as key/value pairs to config/application.yml :

SAMPLE_APP_DATABASE_USER: 'your username'
SAMPLE_APP_DATABASE_PASSWORD: '******'

The environment variables will be available anywhere in your application as ENV variables:

ENV["SAMPLE_APP_DATABASE_USER"]

Here are the remaining ways you can achieve the same.

You can call it anything you want...

username: <%= ENV['CARROTS'] %>
password: <%= ENV['BEANS'] %>

You just have to make sure your deploy script sets the variables CARROTS and BEANS correctly.

try this gem dotenv-rails

add this to Gemfile:

gem 'dotenv-rails', :groups => [:development, :test]

bundle it. Now create a .env file on your apps's directory with following content:

SAMPLE_APP_DATABASE_USER: "devuser"
SAMPLE_APP_DATABASE_PASSWORD: "devuser"

restart the server you're good to go. these variables are exported when you boot your app which you can access in your database.yml file

username: <%= ENV['SAMPLE_APP_DATABASE_USER'] %>
password: <%= ENV['SAMPLE_APP_DATABASE_PASSWORD'] %>

read dotenv-rails documentation for more info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM