djangopostgresqlwebfaction

Django Postgres not reading DB password from environment variable


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"))

Solution

  • 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.