简体   繁体   English

无法使用 AWS RDS 在生产中连接到 postgresql

[英]Not able to connect to postgresql in production, using AWS RDS

my django app, when I ran it locally(localhost + sqlite3) or in production(public Postgres RDS) on port 8000, it works perfectly.我的 django 应用程序,当我在本地(localhost + sqlite3)或在端口 8000 上运行(公共 Postgres RDS)时,它运行良好。

As ports below 1024 are not accessible without root permissions, hence to run on port 80 I used:由于没有 root 权限就无法访问 1024 以下的端口,因此我使用了在端口 80 上运行:

sudo python3 manage.py runserver

and got the following error:并得到以下错误:


OperationalError at /

could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

I am using it from the same region EC2 instance.我在同一个区域 EC2 实例中使用它。 I was able to make migrations and migrate it over the same RDS Postgres database.我能够进行迁移并将其迁移到同一个 RDS Postgres 数据库。

I also tried running on port 80 with gunicorn and root privileges but the same error.我还尝试使用 gunicorn 和 root 权限在端口 80 上运行,但同样的错误。

Here is my production settings.py file:这是我的生产settings.py文件:


import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('SKEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'main_skeleton',
    'storages'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'blog.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['templates',],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'blog.wsgi.application'


# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# SMTP CONFIG

EMAIL_BACKEND = os.environ.get('EMAIL_BACKEND')
EMAIL_HOST = os.environ.get('EMAIL_HOST')
EMAIL_USE_TLS = os.environ.get('EMAIL_USE_TLS')
EMAIL_PORT = os.environ.get('EMAIL_PORT')
EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD')

STATIC_URL = '/static/'

ALLOWED_HOSTS = [os.environ.get('HOST_DOMAIN'), os.environ.get('HOST_IP')]

DATABASES = {

    'default': {

        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.environ.get('DB_NAME'),
        'USER': os.environ.get('DB_USER'),
        'HOST': os.environ.get('DB_HOST'),
        'PASSWORD': os.environ.get('DB_PASSWORD'),
        'PORT': os.environ.get('DB_PORT'),
    }
}

I checked that I am able to get values from env variables as well.我检查了我是否也能够从环境变量中获取值。

Your environment variables are not set inside sudo environment您的环境变量未在 sudo 环境中设置

If you want to preserve environment you can use -E如果你想保护环境,你可以使用-E

man sudo

The -E (preserve environment) option indicates to the security policy that the user wishes to preserve their existing environment variables. -E(保留环境)选项向安全策略表明用户希望保留他们现有的环境变量。 The security policy may return an error if the -E option is specified and the user does not have permission to preserve the environment.如果指定了 -E 选项并且用户没有保留环境的权限,则安全策略可能会返回错误。


Error you are facing is clearly pointing to this ( from django docs )您面临的错误显然指向这一点(来自django 文档

If you're using PostgreSQL, by default (empty HOST), the connection to the database is done through UNIX domain sockets如果您使用的是 PostgreSQL,默认情况下(空 HOST),通过 UNIX 域 sockets 完成与数据库的连接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM