2

I have set the environment variable for DB password in the .bash_profile file but the same is not being read by the PASSWORD field of PostgreSQL. Also, I can print the password in production.py file successfully but the same won't be used by PostgreSQL.

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': '********_db',
    'USER': '*****_name',
    'PASSWORD':os.environ.get("RS_DB_PWD"),
}
#The below will show the password correctly.
print(os.environ.get("RS_DB_PWD"))
Saurabh_Jhingan
  • 190
  • 5
  • 20
  • So when you're setting `PASSWORD` for database `os.environ.get("RS_DB_PWD")` returns `None`? – devaerial Mar 14 '19 at 07:25
  • Does `DATABASES` is somehow overwritten in `production.py` (I assume you break down settings on different modules like `settings/base.py`, `settings/development.py` and `settings/production.py`. – devaerial Mar 14 '19 at 07:40
  • @xbound, This will simply throw an error "no password supplied". Which means it's not able to get the get value from the environment variable. – Saurabh_Jhingan Mar 14 '19 at 07:41
  • 1
    It's probably unneccessary but try to import it to some variable like `DB_PASSWORD = os.environ.get("RS_DB_PWD")` in your `production.py` file and then pass it `'PASSWORD': DB_PASSWORD,` – devaerial Mar 14 '19 at 07:45
  • @xbound, yes I have two settings file, one for production and one for base. But I am intentionally using the production.py file in this case, which in turn is throwing this error. – Saurabh_Jhingan Mar 14 '19 at 07:45
  • 1
    @xbound, I tried to use the DB_PASSWORD theory, but unfortunately, it didn't work out. – Saurabh_Jhingan Mar 14 '19 at 07:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/189999/discussion-between-xbound-and-saurabh-jhingan). – devaerial Mar 14 '19 at 07:47
  • I userally use pipenv and echo PASSWORD to .env file, and use `os.getenv('PASSWORD')` instead of `os.environ.get('PASSWORD')`. – Waket Zheng Mar 14 '19 at 09:32
  • @WaketZheng, thanks for sharing this info. I will give it a try and get back to you. – Saurabh_Jhingan Mar 14 '19 at 09:44

1 Answers1

5

I would try to avoid declaring variables in .bash_profile and would instead create .env file inside your project (add it .gitignore if you use git) where you will put all your secret variables and use tool like Python-Decouple or Dynaconf to read them from .env.

devaerial
  • 2,069
  • 3
  • 19
  • 33