[Fixed]-How to connect Django in EC2 to a Postgres database in RDS?

24πŸ‘

βœ…

All you need to do before doing the normal migration documented in the official tutorial is to be sure to have your RDS instance available and accessible to your EC2 instance.

Then, what you need to do is to modify the settings.py file of your app in the DATABASES section in the following way:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': '<name defined upon RDS creation>',
    'USER': '<username defined upon RDS creation>',
    'PASSWORD': '<password defined upon RDS creation>',
    'HOST': '<this can be found in "Endpoint" on your RDS dashboard, exclude the ":" and the port number>',
    'PORT': '5432',
    }
}

Following this, continue to migrate normally and all should work well.

πŸ‘€bluesummers

0πŸ‘

If you are able to use a deployment service, take a look at at AWS Elastic Beanstalk. It combines EC2, RDS and S3 storage into a Docker and helps keep them together. It’s really easy to connect your RDS instance to your EC2 instance(s). I just launched a Django project using that a few weeks ago.

πŸ‘€awwester

Leave a comment