0

我有一个使用 django 的项目,它部署在应用引擎标准环境中。当我在 django 上使用 cloud_sql_proxy.exe 进行 makemigrations 时,会出现如下错误:

(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

这是我的 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"),
        }
    }

这是我的 .env 文件:

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

如果您查看 .env 文件中的配置,我使用用户myproject而不是postgres用户,而上面的错误说password authentication failed for user "postgres"

如何将默认postgres用户更改为myproject用户?

4

1 回答 1

0

您的配置看起来是正确的,我建议您尝试在 中添加一些print语句settings.py,以便您可以看到程序运行时实际发生的情况,并缩小可能的错误原因。您可以检查的一些事项:

  • 实际存在哪些数据env?我会在你用 初始化它environ.Env(..)之后和你.envenv.read_env(env_file).

  • 他们的哪个分支正在执行?例如,.env调用时是否找到了文件os.path.isfile(env_file)?我会在每个if分支中添加一个打印语句来检查哪些分支正在运行,看看这是否符合您的期望。

  • 什么数据结束DATABASES["default"]?同样,我会检查两次,一次是在调用之后env.db(),一次是在设置HOSTPORTifUSE_CLOUD_SQL_AUTH_PROXY设置之后。

于 2022-01-17T09:20:32.187 回答