0

I have a project using django which is deployed in app engine standard environment. When I use cloud_sql_proxy.exe for makemigrations on django, an error like this appears :

(venv) G:\John Drive\Python\Project\My Project>python manage.py makemigrations
G:\John Drive\Python\Project\venv\lib\site-packages\django\core\management\commands\makemigrations.py:105: RuntimeWarning: Got an error checking a consistent migration history performed for database connection 'default': connection to server at "localhost" (::1), port 5432 failed: Connection refused (0x0000274D/10061)
    Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL:  password authentication failed for user "postgres"

warnings.warn(
No changes detected

This is my settings.py

# [START gaestd_py_django_secret_config]
env = environ.Env(DEBUG=(bool, False))
env_file = os.path.join(BASE_DIR, ".env")

if os.path.isfile(env_file):
    # Use a local secret file, if provided

    env.read_env(env_file)
# [START_EXCLUDE]
elif os.getenv("TRAMPOLINE_CI", None):
    # Create local settings if running with CI, for unit testing

    placeholder = (
        f"SECRET_KEY=a\n"
        f"DATABASE_URL=sqlite://{os.path.join(BASE_DIR, 'db.sqlite3')}"
    )
    env.read_env(io.StringIO(placeholder))
# [END_EXCLUDE]
elif os.environ.get("GOOGLE_CLOUD_PROJECT", None):
    # Pull secrets from Secret Manager
    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.")
# [END gaestd_py_django_secret_config]

# Database
# [START db_setup]
# [START gaestd_py_django_database_config]
# Use django-environ to parse the connection string
DATABASES = {"default": env.db()}

# If the flag as been set, configure to use proxy
if os.getenv("USE_CLOUD_SQL_AUTH_PROXY", None):
    DATABASES["default"]["HOST"] = "127.0.0.1"
    DATABASES["default"]["PORT"] = 5432

# [END gaestd_py_django_database_config]
# [END db_setup]

# Use a in-memory sqlite3 database when testing in CI systems
# TODO(glasnt) CHECK IF THIS IS REQUIRED because we're setting a val above
if os.getenv("TRAMPOLINE_CI", None):
    DATABASES = {
        "default": {
            "ENGINE": "django.db.backends.sqlite3",
            "NAME": os.path.join(BASE_DIR, "db.sqlite3"),
        }
    }

This is my .env file :

DATABASE_URL=postgres://myproject:project123@//cloudsql/myproject-676524:asia-southeast1:myproject/myproject-db
GS_BUCKET_NAME=myproject-676524_myproject
SECRET_KEY=secret123

If you look at the config in the .env file I use the user myproject instead of the postgres user, while the error above says password authentication failed for user "postgres"

How to change the default postgres user to myproject user?

Aras121
  • 81
  • 14

2 Answers2

0

Your configuration looks correct, I would suggest to try adding some print statements to the settings.py, so that you could see what is actually happening when the program runs, and to narrow down possible causes of the error. Some things you could check for:

  • What data is actually present in env? I would check this both after you initialize it with environ.Env(..), and after you read the .env file with env.read_env(env_file).

  • Which of their branches are executing? For example, is the .env file being found when you call os.path.isfile(env_file)? I would add a print statement to each of the if branches to check which ones are being run, and see if this matched your expectations.

  • What data ends up in DATABASES["default"]? Again, I would check this twice, once after calling env.db(), and again after setting HOST and PORT if USE_CLOUD_SQL_AUTH_PROXY is set.

Mabel A.
  • 1,775
  • 4
  • 14
0

i ended deleting/recloning the repo and this error went away, no idea why, but poof no more error

merhoo
  • 589
  • 6
  • 18