Search code examples
pythondjangooracle12c

Django - ERROR: ORA-01017: invalid username/password; logon denied


I am using an Oracle db backend in my django project. I am able to connect to my remote Oracle db using sqlplus from the CMD line and using an Oracle SQL developer. Whenever I try to use python manage.py dbshell it returns the oracle error.

SP2-0751: Unable to connect to Oracle.  Exiting SQL*Plus
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\commands\dbshell.py", line 22, in handle
    connection.client.runshell()
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\oracle\client.py", line 12, in runshell
    subprocess.check_call(args)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 341, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['sqlplus', '-L', 'system/\\"password\\"@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=host)(PORT=8080))(CONNECT_DATA=(SID=temp)))']' returned non-zero exit status 1.

Settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.oracle',
        'NAME': 'temp',
        'USER': 'system',
        'PASSWORD': 'password',
        'HOST': 'host',
        'PORT': '8080',
    }
}

I modified the DB information for security purposes but the settings.py information should match the stack trace error.

Any suggestions as to what I should try or maybe I am forgetting to set something?


Solution

  • I'm not sure how to fix it in Django, but the problem seems to be the way the password is being escaped. You can test this by using the sqlplus command directly based on the failed subprocess output.

    This fails:

    # From error: [sqlplus', '-L', 'user/\\"password\\"@some_dsn
    sqlplus -L user/\\"password\\"@some_dsn
    

    This works:

    sqlplus -L user/password@some_dsn
    

    Might explore submitting a patch for oracle/sqlplus in Django, but I don't see this working any time soon. You can wiggle all the Django knobs, I think this is a low-level issue you won't fix by adjusting your settings.