﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26698	dbshell crashes on Postgres backend with empty database name	Ilya Semenov	mieciu	"Django database backend `postgresql_psycopg2` works perfectly fine without specifying database `NAME` (falling back to the user name for the database name) until you run `manage.py dbshell`, when it crashes.

Please see the test case below.

{{{
root@3a46382913a8:/# /app/src/manage.py shell
>>> from django.conf import settings
>>> pprint(settings.DATABASES['default'])
{'ATOMIC_REQUESTS': False,
 'AUTOCOMMIT': True,
 'CONN_MAX_AGE': 0,
 'ENGINE': 'django.db.backends.postgresql_psycopg2',
 'HOST': '172.17.0.3',
 'NAME': None,
 'OPTIONS': {},
 'PASSWORD': 'xxx',
 'PORT': '5432',
 'TEST': {'CHARSET': None, 'COLLATION': None, 'MIRROR': None, 'NAME': None},
 'TIME_ZONE': None,
 'USER': 'postgres'}
>>> from django.db import connection
>>> c=connection.cursor(); c.execute(""SELECT 1""); print(c.fetchone())
(1,)
}}}

but:

{{{
root@3a46382913a8:/# /app/src/manage.py dbshell
Traceback (most recent call last):
  File ""/app/src/manage.py"", line 10, in <module>
    execute_from_command_line(sys.argv)
  File ""/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py"", line 353, in execute_from_command_line
    utility.execute()
  File ""/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py"", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File ""/usr/local/lib/python3.5/dist-packages/django/core/management/base.py"", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File ""/usr/local/lib/python3.5/dist-packages/django/core/management/base.py"", line 399, in execute
    output = self.handle(*args, **options)
  File ""/usr/local/lib/python3.5/dist-packages/django/core/management/commands/dbshell.py"", line 19, in handle
    connection.client.runshell()
  File ""/usr/local/lib/python3.5/dist-packages/django/db/backends/postgresql/client.py"", line 66, in runshell
    DatabaseClient.runshell_db(self.connection.settings_dict)
  File ""/usr/local/lib/python3.5/dist-packages/django/db/backends/postgresql/client.py"", line 46, in runshell_db
    _escape_pgpass(name) or '*',
  File ""/usr/local/lib/python3.5/dist-packages/django/db/backends/postgresql/client.py"", line 13, in _escape_pgpass
    return txt.replace('\\', '\\\\').replace(':', '\\:')
AttributeError: 'NoneType' object has no attribute 'replace'
}}}
"	Bug	closed	Core (Management commands)	1.9	Normal	fixed	dbshell		Ready for checkin	1	0	0	0	1	0
