summaryrefslogtreecommitdiff
path: root/django/db/backends/postgresql/base.py
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2018-04-03 22:21:47 +0200
committerGitHub <noreply@github.com>2018-04-03 22:21:47 +0200
commit816b386d41e58c6ae750f2e57b6614310886a61a (patch)
treecf5af687795da7c38fab6164e1f08878d6447b5c /django/db/backends/postgresql/base.py
parentb2678468aee3526ec2d092a2f20b1d12b15ba12f (diff)
Refs #24791 -- Made PostgreSQL's nodb connection use first PostgresSQL db when 'postgres' db isn't available.
Thanks Tim Graham and Claude Paroz for reviews.
Diffstat (limited to 'django/db/backends/postgresql/base.py')
-rw-r--r--django/db/backends/postgresql/base.py18
1 files changed, 9 insertions, 9 deletions
diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py
index 0c060e57eb..3537270897 100644
--- a/django/db/backends/postgresql/base.py
+++ b/django/db/backends/postgresql/base.py
@@ -9,7 +9,7 @@ import warnings
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
-from django.db import DEFAULT_DB_ALIAS
+from django.db import connections
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.utils import DatabaseError as WrappedDatabaseError
from django.utils.functional import cached_property
@@ -255,16 +255,16 @@ class DatabaseWrapper(BaseDatabaseWrapper):
"to avoid running initialization queries against the production "
"database when it's not needed (for example, when running tests). "
"Django was unable to create a connection to the 'postgres' database "
- "and will use the default database instead.",
+ "and will use the first PostgreSQL database instead.",
RuntimeWarning
)
- settings_dict = self.settings_dict.copy()
- settings_dict['NAME'] = settings.DATABASES[DEFAULT_DB_ALIAS]['NAME']
- nodb_connection = self.__class__(
- self.settings_dict.copy(),
- alias=self.alias,
- allow_thread_sharing=False,
- )
+ for connection in connections.all():
+ if connection.vendor == 'postgresql' and connection.settings_dict['NAME'] != 'postgres':
+ return self.__class__(
+ {**self.settings_dict, 'NAME': connection.settings_dict['NAME']},
+ alias=self.alias,
+ allow_thread_sharing=False,
+ )
return nodb_connection
@cached_property