使用Django在另一台服务器上创建PostgreSQL数据库

时间:2017-10-25 18:23:56

标签: python django linux postgresql ubuntu

我有一个工作的本地环境,使用Django 1.11和在Linux Mint上运行的PostgreSQL 10数据库。

我正在尝试获取此环境的副本并将其安装在两个Ubuntu 16.04开发服务器上:一个用于Web服务器,另一个用于SQL服务器。

问题是使用Ubuntu PostgreSQL服务器运行Django。

当我获取Django settings.py文件,并将服务器从“localhost”更改为新PostgreSQL服务器的IP地址时,使用manage.py发出的每个命令都会失败。这包括createsuperuser,makemigrations和migrate。这在我的工作,开发机器和新的Ubuntu Web服务器上都失败了。

我是否缺少应该在更改数据库服务器之后和运行manage.py命令之前完成的步骤?

我读过的所有内容都说只需运行makemigrations,然后迁移命令。我甚至试图删除makemigrations文件夹,但仍然没有运气。

已验证:可以使用相同的用户ID和密码远程连接到新的PostgreSQL服务器,并且能够创建表(因此问题不应与安全性或远程访问相关)。

以下是每次出现的错误:

            Traceback (most recent call last):
  File "/home/username/Documents/pycharm-2017.2.3/helpers/pycharm/django_manage.py", line 43, in <module>
    run_module(manage_file, None, '__main__', True)
  File "/usr/lib/python3.5/runpy.py", line 196, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/dcripps/PycharmProjects/seamus/manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/core/management/base.py", line 327, in execute
    self.check()
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 62, in _run_checks
    issues.extend(super(Command, self)._run_checks(**kwargs))
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/core/checks/urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/core/checks/urls.py", line 26, in check_resolver
    return check_method()
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/urls/resolvers.py", line 254, in check
    for pattern in self.url_patterns:
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/urls/resolvers.py", line 405, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/dcripps/PycharmProjects/seamus/seamus/urls.py", line 21, in <module>
    from assessments import views as assessment_views
  File "/home/dcripps/PycharmProjects/seamus/assessments/views.py", line 10, in <module>
    from .forms import NewForm, CreateForm, DetailForm, EquipmentDetailForm, EquipmentAddForm, InspectionsForm, \
  File "/home/dcripps/PycharmProjects/seamus/assessments/forms.py", line 17, in <module>
    .values_list('item_number', 'number_type'))
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/db/models/query.py", line 250, in __iter__
    self._fetch_all()
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/db/models/query.py", line 1118, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/db/models/query.py", line 138, in __iter__
    for row in compiler.results_iter():
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 836, in results_iter
    results = self.execute_sql(MULTI)
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 894, in execute_sql
    raise original_exception
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 884, in execute_sql
    cursor.execute(sql, params)
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/db/backends/utils.py", line 80, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "EquipmentTypes" does not exist
LINE 1: ...ntTypes"."equipment_type")) AS "number_type" FROM "Equipment..

谢谢。

    Firewall on SQL Server:
    :~$ sudo ufw status
    Status: active
    To                         Action      From
    --                         ------      ----
    5432                       ALLOW       Anywhere  

以下是我添加到pg_hba.conf文件中的内容: 主持所有0.0.0.0/0 md5

settings.py中的数据库:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': '******',
            'USER': '******',
            'PASSWORD': '******',
            'HOST': '192.168.1.221',
            'PORT': '5432',
        }

}

根据丹尼尔,错误来自:

item_number_list = [("", "---------")] + \
                   list(EquipmentTypes.objects.all()
                        .annotate(number_type=Concat('item_number', Value(' - '), 'equipment_type'))
                        .values_list('item_number', 'number_type'))

我使用它作为填充模型中下拉列表的方法:

'item_number': forms.Select(choices=item_number_list, attrs={'class': 'form-control'}),

我注释掉了这段代码,一切都很完美。

1 个答案:

答案 0 :(得分:1)

回溯显示问题是由assessment / forms.py的第17行引起的 - 我们无法看到实际的代码,但似乎您在类或模块级别执行查询。您不能这样做,因为在导入时执行该级别的任何操作,这是在迁移有机会运行之前。

请注意,此问题与在特定服务器上运行数据库无关,而只是由于移动到新的空数据库而导致。