4

I am not trying to check the db name to the connection, I have multiple database setup in django and also used the database route to create a failover but I am wondering if I can get the name of the connection host or the name given to the settings...

for example

DATABASES = {
    'default': {  # trying to get this name 'default'
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_name',
        'USER': 'euser',
        'PASSWORD': 'password',
        'HOST': 'host', # trying to get this host name 'host'
        'PORT': 3306,
    },
    'node2': {  # trying to get this name 'node2'
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_name',
        'USER': 'euser',
        'PASSWORD': 'password',
        'HOST': 'host2',  # trying to get this host name 'host2'
        'PORT': 3306,
    },
}

Is it possible to get those names I commented out above? For my failover, I am trying to send an email if another db is being used, but it would be great if I can get especially the host so it would be easier on me

P.S. off topic, is it possible to change the name default to other names? I tried changing it and I would get errors. Seems like I must have at least one database setting name called default

Thanks in advance for any help.

7
  • I don't know much about failovers or having multiple DATABASES, but looking at this other answer looks like it should be possible to get your connection's db name? Commented Jan 6, 2018 at 0:00
  • Is DATABASES set in a config file somewhere ? Commented Jan 6, 2018 at 0:22
  • @BorrajaX that's the one I read already, but it only gives the database name, as mentioned database name isn't what I want. I want to get either the default setting's name or host Commented Jan 6, 2018 at 0:26
  • @JacobIRR in a different file which can be used to differ dev and production settings Commented Jan 6, 2018 at 0:27
  • 2
    If you could get the connecton name, shouldn't this do: from django.conf import settings; settings.DATABASES[connection.settings_dict['NAME']]['HOST'] ? (or what seems simpler if connection.settings_dict happens to be the same dict you have in the Django settings) just connection.settings_dict['HOST']? Commented Jan 6, 2018 at 0:28

1 Answer 1

11

So, doing:

import pprint  # Makes it sooo pretty :)
from django.db import connection
print(pprint.pformat(connection.settings_dict))

Will give you the current connection settings. For instance, the code above should print something like that:

{'ATOMIC_REQUESTS': False,
 'AUTOCOMMIT': True,
 'CONN_MAX_AGE': 600,
 'ENGINE': 'django.db.backends.postgresql',
 'HOST': 'localhost',
 'NAME': 'stack_overflow',
 'OPTIONS': {},
 'PASSWORD': '***',
 'PORT': '5432',
 'TEST': {'CHARSET': None, 'COLLATION': None, 'MIRROR': None, 'NAME': None},
 'TEST_NAME': 'stack_overflow_test',
 'TIME_ZONE': None,
 'USER': 'postgres'}

So you should be able to access the hostname just by doing connection.settings_dict['HOST']

Sign up to request clarification or add additional context in comments.

Comments

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.