1

I just want to connect my local oracle db with my django project but my database credential is not working. Actually, I'm able to connect my oracle database via sql developer with that credential: enter image description here

I just used that credential in django settings_py like that

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.oracle',
        'NAME': 'INTERNAL',
        'USER': 'system',
        'PASSWORD': 'oracle',        
        'HOST':'localhost/xe',
        'PORT':'1521'
    }
}

and error is:

Traceback (most recent call last):
web_1  |   File "manage.py", line 22, in <module>
web_1  |     execute_from_command_line(sys.argv)
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
web_1  |     utility.execute()
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
web_1  |     self.fetch_command(subcommand).run_from_argv(self.argv)
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
web_1  |     self.execute(*args, **cmd_options)
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
web_1  |     output = self.handle(*args, **options)
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/makemigrations.py", line 110, in handle
web_1  |     loader.check_consistent_history(connection)
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/migrations/loader.py", line 282, in check_consistent_history
web_1  |     applied = recorder.applied_migrations()
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 65, in applied_migrations
web_1  |     self.ensure_schema()
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 52, in ensure_schema
web_1  |     if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 254, in cursor
web_1  |     return self._cursor()
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 229, in _cursor
web_1  |     self.ensure_connection()
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
web_1  |     self.connect()
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
web_1  |     six.reraise(dj_exc_type, dj_exc_value, traceback)
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
web_1  |     raise value.with_traceback(tb)
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
web_1  |     self.connect()
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 189, in connect
web_1  |     self.connection = self.get_new_connection(conn_params)
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/oracle/base.py", line 212, in get_new_connection
web_1  |     return Database.connect(self._connect_string(), **conn_params)
web_1  | django.db.utils.DatabaseError: ORA-12545: Connect failed because target host or object does not exist

here its my listener status

 Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC_FOR_XE)))
    STATUS of the LISTENER
    ------------------------
    Alias                     LISTENER
    Version                   TNSLSNR for Linux: Version 11.2.0.2.0 - Production
    Start Date                28-DEC-2017 15:51:21
    Uptime                    0 days 2 hr. 8 min. 36 sec
    Trace Level               off
    Security                  ON: Local OS Authentication
    SNMP                      OFF
    Default Service           XE
    Listener Parameter File   /u01/app/oracle/product/11.2.0/xe/network/admin/listener.ora
    Listener Log File         /u01/app/oracle/diag/tnslsnr/e48c7c272f44/listener/alert/log.xml
    Listening Endpoints Summary...
      (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC_FOR_XE)))
      (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=e48c7c272f44)(PORT=1521)))
      (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=e48c7c272f44)(PORT=8080))(Presentation=HTTP)(Session=RAW))
    Services Summary...
    Service "PLSExtProc" has 1 instance(s).
      Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
    Service "XE" has 1 instance(s).
      Instance "XE", status READY, has 1 handler(s) for this service...
    Service "XEXDB" has 1 instance(s).
      Instance "XE", status READY, has 1 handler(s) for this service...
     here its my listener status
3
  • I notice that the port in your Django configurations does not match the port in your SQL Developer Connection Dialogue (1521 vs 8080). Perhaps that is the issue? Commented Dec 28, 2017 at 17:28
  • i just tried many combinations im sorry i edited my question Commented Dec 28, 2017 at 17:33
  • actually how can i give my sid to settings ? Commented Dec 28, 2017 at 17:49

2 Answers 2

5

You should change HOST to localhost' or '127.0.0.1 and SID is NAME.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.oracle',
        'NAME': 'xe',
        'USER': 'system',
        'PASSWORD': 'oracle',        
        'HOST':'127.0.0.1',
        'PORT':'1521'
    }
}

For future references, if Oracle is configured with Service name instead of SID, then the configuration would be:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.oracle',
        'NAME': '127.0.0.1:1521/service.name',
        'USER': 'system',
        'PASSWORD': 'oracle',        
    }
}

Another thing to consider when working with Oracle in Django is that when you connect to Other Users (schema) database, you have to set db_table Meta option in Django models:

class OracleTable(models.Model):
    ... fields ...
    class Meta:
        db_table = '\"OTHERUSER\".\"ORACLETABLE\"'
Sign up to request clarification or add additional context in comments.

7 Comments

i tried your suggestion but now error turns 'django.db.utils.DatabaseError: ORA-12541: TNS:no listener'
also we didn't give the databasename
Ok, did you install cx_oracle and Oracle Client? This error says it cannot find the database at 127.0.0.1:1521.
i added my listener status to question
Unfortunately, I don't have much experience with Oracle running on Linux. Listener is running, but looks like Django/server cannot access it.
|
0

NAME must be the dsn in cx_Oracle.connect, internally 'django.db.backends.oracle' try to connect with:

import cx_Oracle
cx_Oracle.connect(user=DATABASES['default']['USER'], password=DATABASES['default']['PASSWORD'], dsn=DATABASES['default']['NAME'])

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.