简体   繁体   中英

Not able to access the django test database

I try to do some testing in django on the deployment server. I am trying to run a django.test.TestCase test - the test doesn't do anything yet. I have given the test database a name:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydatabase',
        'USER': 'User1',
        'PASSWORD': 'XXXXXX',
        'HOST': '',
        'PORT': '',
        'TEST_NAME': 'test_mydatabase',
    }
}

However, if I run the tests I always get an exception. The test seems to try to access a database called "postgres".

Creating test database for alias 'default'...
    Traceback (most recent call last):
      File "./manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
      File "/usr/lib/python2.7/dist-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
        utility.execute()
      File "/usr/lib/python2.7/dist-packages/django/core/management/__init__.py", line 377, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/usr/lib/python2.7/dist-packages/django/core/management/commands/test.py", line 50, in run_from_argv
        super(Command, self).run_from_argv(argv)
      File "/usr/lib/python2.7/dist-packages/django/core/management/base.py", line 288, in run_from_argv
        self.execute(*args, **options.__dict__)
      File "/usr/lib/python2.7/dist-packages/django/core/management/commands/test.py", line 71, in execute
        super(Command, self).execute(*args, **options)
      File "/usr/lib/python2.7/dist-packages/django/core/management/base.py", line 338, in execute
        output = self.handle(*args, **options)
      File "/usr/lib/python2.7/dist-packages/django/core/management/commands/test.py", line 88, in handle
        failures = test_runner.run_tests(test_labels)
      File "/usr/lib/python2.7/dist-packages/django/test/runner.py", line 147, in run_tests
        old_config = self.setup_databases()
      File "/usr/lib/python2.7/dist-packages/django/test/runner.py", line 109, in setup_databases
        return setup_databases(self.verbosity, self.interactive, **kwargs)
      File "/usr/lib/python2.7/dist-packages/django/test/runner.py", line 299, in setup_databases
        serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
      File "/usr/lib/python2.7/dist-packages/django/db/backends/creation.py", line 362, in create_test_db
        self._create_test_db(verbosity, autoclobber)
      File "/usr/lib/python2.7/dist-packages/django/db/backends/creation.py", line 455, in _create_test_db
        with self._nodb_connection.cursor() as cursor:
      File "/usr/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 167, in cursor
        cursor = utils.CursorWrapper(self._cursor(), self)
      File "/usr/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 138, in _cursor
        self.ensure_connection()
      File "/usr/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 133, in ensure_connection
        self.connect()
      File "/usr/lib/python2.7/dist-packages/django/db/utils.py", line 94, in __exit__
        six.reraise(dj_exc_type, dj_exc_value, traceback)
      File "/usr/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 133, in ensure_connection
        self.connect()
      File "/usr/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 122, in connect
        self.connection = self.get_new_connection(conn_params)
      File "/usr/lib/python2.7/dist-packages/django/db/backends/postgresql_psycopg2/base.py", line 130, in get_new_connection
        connection = Database.connect(**conn_params)
      File "/usr/lib/python2.7/dist-packages/psycopg2/__init__.py", line 179, in connect
        connection_factory=connection_factory, async=async)
    django.db.utils.OperationalError: FATAL:  no pg_hba.conf entry for host "[local]", user "User1", database "postgres", SSL off

How can I resolve this? Any clues would be appreciated.

Django需要具有创建数据库的权限,否则测试将无法进行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM