0

Well, I had one setting file before and it was working perfectly both in local and AWS of course with both the database configuration in one setting file. When ever I use to push my code on AWS I would comment my local db configuration and then push. So for sure, that's annoying. I can't comment and uncomment constantly both the database configuration in one file.

Therefore, I decided to have 2 setting files. one for the local and the other for AWS.

Once I pulled the code into AWS server and run migrations

python manage.py migrate --settings=settings.staging

It worked and migrated. By the way, this is the setting file which resides my RDS configuration. Now the moment I hit the endpoints via postmant the output is as

OperationalError at /account/v1/login
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?

means it is still reading the default settings file. How come I make the server run this particular staging setting file. Do I have to declare it on nginx, supervisor or gunicorn? I'm using these 3 services as well.

Below is my settings file for staging.

from .base import *

# --------------- AWS RDS ---------------
DATABASES = {
    'default': {
        # 'ENGINE': 'django.db.backends.sqlite3',
        # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'db name here',
        'HOST': 'RDS host here',
        'USER': 'postgres',
        'PASSWORD': 'pass here',
        'PORT': '5432'
    }
}

This is my complete staging.py file. which only have this while the rest of the setting is being imported from base.py which is a default setting file. Also it has the local settings.

Any recommendations?

This is also what I've tried.

import socket

hostname = socket.gethostname()
if hostname == "staging":
     from settings.staging import *

Thank you

1 Answer 1

1

The way I solved this issue is using hostnames. We have 1 settings file with all the default settings. We then import specific files depending on the hostname. Of course you could also use things like IAM-instance roles or something.

We would have this in the default settings file:

import socket
DATABASE = {'default': {'ENGINE': 'django.db.backends.sqlite3', ...}}
hostname = socket.gethostname()
if hostname == "staging-blabla"
    from staging import *

staging.py would contain the following:

DATABASE = {'default': {'ENGINE': 'django.db.backends.postgresql_psycopg2', ...}}
Sign up to request clarification or add additional context in comments.

6 Comments

hi there, thank you for your answer. but I am still facing the same issue on server which says 'Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?' . Any Idea?
Are you using PostGres local too? Or SQLite?
Postgres local yes. and RDS
as said it was working perfectly before I had separate file for staging
Can you show the import logic you use to include the staging settings file?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.