                  "Django was unable to create a connection to the 'postgres' database "
                  "and will use the default 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,
              )
          return nodb_connection
  
      @cached_property
      def pg_version(self):
          with self.temporary_connection():
 
</s> remove                 "and will use the default database instead.",
 </s> add                 "and will use the first PostgreSQL database instead.", </s> remove                 warnings.simplefilter('always', RuntimeWarning)
                nodb_conn = connection._nodb_connection
 </s> add                 with mock.patch.object(
                    connection,
                    'settings_dict',
                    {**connection.settings_dict, 'NAME': 'postgres'},
                ):
                    warnings.simplefilter('always', RuntimeWarning)
                    nodb_conn = connection._nodb_connection </s> remove         self.assertEqual(nodb_conn.settings_dict['NAME'], connection.settings_dict['NAME'])
 </s> add         self.assertEqual(nodb_conn.settings_dict['NAME'], connections['other'].settings_dict['NAME']) </s> remove from django.db import DatabaseError, connection
 </s> add from django.db import DatabaseError, connection, connections </s> remove from django.db import DEFAULT_DB_ALIAS
 </s> add from django.db import connections
            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,
                    )
                  "Django was unable to create a connection to the 'postgres' database "
                  "and will use the default database instead.",
                  RuntimeWarning
              )
            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,
                    )
            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,
                    )
            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,
                    )
            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,
                    )
            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,
                    )
            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,
                    )
            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
      def pg_version(self):
          with self.temporary_connection():
keep keep keep keep replace replace replace replace replace replace replace keep keep keep keep keep
 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. 
django/db/backends/postgresql/base.py