0

I'm trying to host my Django app on the Google app engine. I tested the app locally using a .env file for the environmental variables, but when I moved the variables to the secret manager, the app could not access my database URL and other app secrets.

Below is my code snippet that retrieves the secrets from the secret manager in my settings.py file:

env = environ.Env(DEBUG=(bool, False))
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
env_file = os.path.join(BASE_DIR, ".env")

if os.path.isfile(env_file):
    env.read_env(env_file)
elif os.environ.get("GOOGLE_CLOUD_PROJECT", None):
    project_id = os.environ.get("GOOGLE_CLOUD_PROJECT")
    client = secretmanager.SecretManagerServiceClient()
    settings_name = os.environ.get("SETTINGS_NAME", "django_settings")
    name = f"projects/{project_id}/secrets/{settings_name}/versions/latest"
    payload = client.access_secret_version(name=name).payload.data.decode("UTF-8")
    env.read_env(io.StringIO(payload))
else:
    raise Exception("No local .env or GOOGLE_CLOUD_PROJECT detected. No secrets found.")

when I include this line: print("Database URL:", os.environ.get('DATABASE_URL')) to check what database URL is being accessed by the Django environment, I'm getting a URL that I used in a past version of my secrets manager which includes a database that I have since deleted.

The error I'm receiving is this:

OperationalError: connection to server at "127.0.0.1", port 5432 failed: FATAL:  database "my database-name" does not exist

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.