django - 无法访问 Django 测试数据库

标签 django postgresql

我尝试在部署服务器上使用 Django 进行一些测试。我正在尝试运行 django.test.TestCase 测试 - 该测试尚未执行任何操作。我给测试数据库起了个名字:

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

但是,如果我运行测试,我总是会遇到异常。该测试似乎试图访问名为“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

我该如何解决这个问题?任何线索将不胜感激。

最佳答案

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

关于django - 无法访问 Django 测试数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30014464/

相关文章:

sql - 将时间戳截断为任意间隔

多应用Django打包项目

javascript - 将音频从浏览器保存到后端(Django)

django - 如何在 Django 中使用 DeleteView 显示相关项目?

postgresql - 如何在Postgres数据库中添加模式描述?

postgresql - SQL 查询在 pentaho 报表设计器中不工作,而相同的查询在 postgresql 中工作正常

django - 将客户端 IP 信息添加到 Django 记录器中

python - 如何在 Django 中获取带有用户 ID 的 ManyToManyField 对象?

php - 使用 PHP 的 Postgres 备份数据

postgresql - 如何在postgres中获得除负数之外的平均值