I have created an EC2 instance on AWS and deployed my django application on it. 

Also created RDS postgres database on AWS. Whenever I access my public ip, I get the following error,

> 
> OperationalError at /
> 
> 
> could not connect to server: Connection refused Is the server running
>  on host "localhost" (127.0.0.1) and accepting TCP/IP connections on
>  port 5432?
> 
> 
> 

Both EC2 and RDS instances are connected. Verified by following,

* Connected RDS postgres using pgadmin4 on my local PC.
* Created supersuser on ec2 console and it populated the postgres.

**Nginx configuration:**

```
server {
  listen 80;
  server_name 18.218.45.241;
  location = /favicon.ico { access_log off; log_not_found off; }
  location /static/ {
      root /home/ubuntu/Crowdsocial_project;
  }
  location / {
      include proxy_params;
      proxy_pass http://unix:/home/ubuntu/Crowdsocial_project/crowdsocial.sock;
  }
}

```

**Gunicorn configuration:**

```
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/Crowdsocial_project
ExecStart=/home/ubuntu/Crowdsocial_project/venv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/Crowdsocial_project/crowdsocial.sock main.wsgi:application
[Install]
WantedBy=multi-user.target

```

**EC2 Security group:**

[![enter image description here](https://i.stack.imgur.com/GXQXz.png)](https://i.stack.imgur.com/GXQXz.png)

[![enter image description here](https://i.stack.imgur.com/Z86s5.png)](https://i.stack.imgur.com/Z86s5.png)

**RDS Security group:**

[![enter image description here](https://i.stack.imgur.com/1ggNN.png)](https://i.stack.imgur.com/1ggNN.png)

[![enter image description here](https://i.stack.imgur.com/N5cn9.png)](https://i.stack.imgur.com/N5cn9.png)

**Settings.py**

```
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates')
STATIC_DIR = os.path.join(BASE_DIR,'static')
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    STATIC_DIR,
]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'secret'

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

ALLOWED_HOSTS = ['18.218.45.241']

DATE_INPUT_FORMATS= ['%Y-%m-%d',]

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'example@gmail.com'
EMAIL_HOST_PASSWORD = 'pass'
EMAIL_PORT = 587

# Application definition

INSTALLED_APPS = [
    'users.apps.UsersConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'bootstrap_modal_forms',

    'main',
    'first_app',
    'campaign',
    'invoice',
    'taggit',
    'corsheaders',
    'taggit_selectize',
    'rest_framework',
    'django_filters',
    'django_extensions',
    'shop',
    'search',
    'cart',
    'orders',
    'widget_tweaks',
    'billing',
]

REST_FRAMEWORK = {
    'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',)
}

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
]

AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.AllowAllUsersModelBackend']

CORS_ORIGIN_ALLOW_ALL = True

ROOT_URLCONF = 'main.urls'

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

WSGI_APPLICATION = 'main.wsgi.application'
AUTH_USER_MODEL = 'users.CustomUser'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'name',
        'USER': 'user',
        'PASSWORD': 'pass',
        'HOST': 'crowdsocial-postgres.c9sefqws77mc.us-east-2.rds.amazonaws.com',
        'PORT': '5432',
    }
}

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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

LOGIN_URL = 'users:login'
# LOGIN_REDIRECT_URL = 'home'
LOGOUT_REDIRECT_URL = 'first_app:home'

```