2

I have a working local environment using Django 1.11 with a PostgreSQL 10 database running on Linux Mint.

I am trying to take a copy of this environment and install it on two Ubuntu 16.04 development servers: one for the web server and the other for the SQL server.

The problem is running Django with the Ubuntu PostgreSQL server.

When I take the Django settings.py file, and change the server from 'localhost' to the IP address of the new PostgreSQL server, every command issued with manage.py fails. This includes createsuperuser, makemigrations and migrate. This fails on both my working, development machine and the new Ubuntu web server.

Is there a step I am missing that should be done after changing the database server and before running a manage.py command?

Everything I have read says to simply run the makemigrations, and migrate commands. I have even tried to delete the makemigrations folders, but still no luck.

Verified: Can connect to the new PostgreSQL server remotely using the same user ID and password, and was able to create a table (so problem should not be related to security or remote access).

Here is the error that comes up each time:

            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..

Thank you.

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

Here is what I added to the pg_hba.conf file: host all all 0.0.0.0/0 md5

Databases in settings.py:

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

}


As per Daniel, the error came from here:

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

I was using this as a way to populate a dropdown list in a model:

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

I commented out this code, and everything worked perfectly.

2
  • You need to show your DATABASES setting. Also, what command are you running to get that error? Commented Oct 25, 2017 at 18:26
  • 1
    Can you share your pg_hba.conf? Do you have any firewall. And please show your Django config Commented Oct 25, 2017 at 18:26

1 Answer 1

1

The traceback is showing that the problem is caused by line 17 of assessment/forms.py - we can't see the actual code but it seems likely that you are performing a query at class or module level. You must not do this, as anything at that level is executed on import, which is before the migrations can have a chance to run.

Note that this problem is not related to running the database on a separate server specifically, but just caused by moving to a new empty database.

Sign up to request clarification or add additional context in comments.

4 Comments

This worked perfectly, thank you Daniel. I had some queries to act as drop down lists in some forms, so will have to figure out a different way to do that. Thanks again.
You might need to move them into the form's __init__ method.
I have multiple forms using the same list. Is there a way to write the list once, and have multiple forms use it, or does it need to be coded in each form's init method?
Really for this sort of thing you should use a ModelChoiceField. To get your customised representation, you should subclass that field and override label_from_instance as described in the docs (note that means doing the concatenation in Python, but I don't think it matters).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.