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.

Dora
  • 6,776
  • 14
  • 51
  • 99
  • I don't know much about failovers or having multiple `DATABASES`, but looking at [this other answer](https://stackoverflow.com/a/33996973/289011) looks like it should be possible to get your connection's db name? – Savir Jan 06 '18 at 00:00
  • Is `DATABASES` set in a config file somewhere ? – JacobIRR Jan 06 '18 at 00: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` – Dora Jan 06 '18 at 00:26
  • @JacobIRR in a different file which can be used to differ dev and production settings – Dora Jan 06 '18 at 00: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']`? – Savir Jan 06 '18 at 00:28
  • @BorrajaX ah! I missed this part, it's so far down. I only read the one marked as answer and a few more answers down....:| Do you want to post a reply so I can mark as answer? P.S. I think it's `from django.conf import settings` there's typo, typed the other way around ^_^ – Dora Jan 06 '18 at 00:37
  • That's very kind of you. And yes: there was, indeed a typo (I always get confused with the order) – Savir Jan 06 '18 at 00:48

1 Answers1

10

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']

Savir
  • 17,568
  • 15
  • 82
  • 136