user1670773
user1670773

Reputation: 1037

Rails database password as environment variables

This works:

production:
  adapter: mysql2
  encoding: utf8
  host: localhost
  database: myapp
  username: myapp
  password: jksdfUIJsdf

Then on terminal touch tmp/restart.txt.

This does not work:

production:
  adapter: mysql2
  encoding: utf8
  host: localhost
  database: myapp
  username: myapp
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>

Then on terminal

export MYAPP_DATABASE_PASSWORD=jksdfUIJsdf
touch tmp/restart.txt

So if I set the password as plaintext in database.yml file then my application works properly but if I set the password as environment variable with export command, then my application does not work because it gives error password missing. I am using mysql database. How to solve this?

Upvotes: 2

Views: 2826

Answers (2)

David Moles
David Moles

Reputation: 51123

When you touch tmp/restart.txt, you're not starting a new Rails server, you're just telling the existing server to reload itself. The existing server will still have the environment it started with, and will never see any environment variables you set after that time.

Upvotes: 2

Abhilash Reddy
Abhilash Reddy

Reputation: 1549

For Rails app configuration, I use figaro gem.

Add the below line to your Gemfile and do bundle.

gem "figaro"

After that run the below command:

bundle exec figaro install

This will create config/application.yml and also will add it to the .gitignore file.

Now inside config/application.yml enter the credentials.

# config/application.yml

MYAPP_DATABASE_PASSWORD: "2954"

Visit the Github page for more info.

Upvotes: 0

Related Questions