简体   繁体   中英

Django oracle database connection

Im facing issue while connecting to oracle db in django project, Oracle connection details: '''

XXXX=
  (DESCRIPTION =
  (LOAD_BALANCE = YES)
  (FAILOVER = YES)
  (ADDRESS_LIST =
  (ADDRESS = (COMMUNITY = XXX.xxxx)
  (PROTOCOL = TCP)
  (Host = xxx-xx.xxx.com)(Port = 1521)))
  (CONNECT_DATA =
  (service_name = xxxx.xxxxx)))

'''

i want to fit oracle details in below django database connection code,

'''

DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.oracle',
            'NAME': 'xe',
            'USER': 'a_user',
            'PASSWORD': 'a_password',
            'HOST': 'dbprod01ned.mycompany.com',
            'PORT': '1540',
        }
    }

'''

Welcome to StackOverflow!

From the Doc. If tnsnames.ora isn't being used.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.oracle',
        'NAME': '(DESCRIPTION=(LOAD_BALANCE=YES)(FAILOVER=YES)(ADDRESS_LIST=(ADDRESS=(COMMUNITY=XXX.xxxx)(PROTOCOL=TCP)(Host=xxx-xx.xxx.com)(Port=1521)))(CONNECT_DATA=(service_name=xxxx.xxxxx)))',
        'USER': 'a_user',
        'PASSWORD': 'a_password',
        'HOST': '',
        'PORT': '',
    }
}

Or you have made sure your oracle client has tnsnames configured

tnsnames.ora

MY_AWESOME_TNS_ALIAS=(DESCRIPTION=(LOAD_BALANCE=YES)(FAILOVER=YES)(ADDRESS_LIST=(ADDRESS=(COMMUNITY=XXX.xxxx)(PROTOCOL=TCP)(Host=xxx-xx.xxx.com)(Port=1521)))(CONNECT_DATA=(service_name=xxxx.xxxxx)))

Now django can be configured by alias name. You hide the connection-details from the django application.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.oracle',
        'NAME': 'MY_AWESOME_TNS_ALIAS',
        'USER': 'a_user',
        'PASSWORD': 'a_password',
        'HOST': '',
        'PORT': '',
    }
} 

Best of luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM