django从cron启动时出现django/postgresql问题

django从cron启动时出现django/postgresql问题,django,postgresql,Django,Postgresql,编辑以询问谁认为此问题与可能相关的问题有关 我有一个相当简单的django 1.11项目,使用rest_框架,当我从命令行启动它时,输入 nohup python manage.py runserver 0.0.0.0:4448 & 在centos。连接到postgresql数据库,使用 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'my_databa

编辑以询问谁认为此问题与可能相关的问题有关

我有一个相当简单的django 1.11项目,使用rest_框架,当我从命令行启动它时,输入

nohup python manage.py runserver 0.0.0.0:4448 &
在centos。连接到postgresql数据库,使用

DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': 'my_database',
    'USER': 'my_user',
     #'PASSWORD': 'mypassword',
     'HOST': '127.0.0.1',
    ' PORT': '5432',
  }
}
在my settings.py文件中。但是,如果我将runserver命令设置为在启动时从cron运行,则在向应用程序发送请求时会得到以下结果:

... lot of stuff
django.db.utils.OperationalError: could not connect to server: Connection 
refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
在我的pg_hba文件中

IPv4本地连接: 我也有

netstat -plunt | grep post
tcp        0      0 10.2.11.53:5432         0.0.0.0:*               LISTEN      
        867/postmaster      
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      
        867/postmaster  
有什么建议吗

谢谢

a

PS完整回溯:

Unhandled exception in thread started by <function wrapper at 0x1a11230>
Performing system checks...

[<RegexURLPattern batch-batch-done ^batch/batch-done/$>, <RegexURLPattern batch-load-urls ^batch/load-urls/$>, <RegexURLPattern batch-request-batch ^batch/request-batch/$>, <RegexURLPattern batch-schedule-job ^batch/schedule-job/$>]
System check identified no issues (0 silenced).
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/usr/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 128, in inner_run
    self.check_migrations()
  File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 422, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/usr/lib/python2.7/site-packages/django/db/migrations/executor.py", line 20, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/usr/lib/python2.7/site-packages/django/db/migrations/loader.py", line 52, in __init__
    self.build_graph()
  File "/usr/lib/python2.7/site-packages/django/db/migrations/loader.py", line 209, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/usr/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 65, in applied_migrations
    self.ensure_schema()
  File "/usr/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 52, in ensure_schema
    if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
  File "/usr/lib/python2.7/site-packages/django/db/backends/base/base.py", line 254, in cursor
    return self._cursor()
  File "/usr/lib/python2.7/site-packages/django/db/backends/base/base.py", line 229, in _cursor
    self.ensure_connection()
  File "/usr/lib/python2.7/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
  File "/usr/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/lib/python2.7/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
  File "/usr/lib/python2.7/site-packages/django/db/backends/base/base.py", line 189, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/lib/python2.7/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/usr/lib64/python2.7/site-packages/psycopg2/__init__.py", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: Connection refused
        Is the server running on host "127.0.0.1" and accepting
        TCP/IP connections on port 5432?

你可以发布完整的回溯,以及cron作业本身吗


另外,如果我正确理解了您的问题,您是否正在尝试使用cron在引导时启动服务器?如果是这种情况,最好使用类似supervisord的工具来管理服务器进程。

为什么要从cron运行开发服务器?在生产环境中,您应该使用真正的wsgi服务器,由系统的流程管理器(如systemd)管理。完整回溯的可能重复:看起来我无法删除注释。发布追踪的最佳方式是什么。评论部分太大。你可能需要编辑你的原始帖子评论太小,无法进行回溯。我不知道为什么我的设置不起作用。我实际上是从其他人那里继承的,但使用主管是一个巨大的改进,比我想象的更容易。解决了这个问题。
Unhandled exception in thread started by <function wrapper at 0x1a11230>
Performing system checks...

[<RegexURLPattern batch-batch-done ^batch/batch-done/$>, <RegexURLPattern batch-load-urls ^batch/load-urls/$>, <RegexURLPattern batch-request-batch ^batch/request-batch/$>, <RegexURLPattern batch-schedule-job ^batch/schedule-job/$>]
System check identified no issues (0 silenced).
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/usr/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 128, in inner_run
    self.check_migrations()
  File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 422, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/usr/lib/python2.7/site-packages/django/db/migrations/executor.py", line 20, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/usr/lib/python2.7/site-packages/django/db/migrations/loader.py", line 52, in __init__
    self.build_graph()
  File "/usr/lib/python2.7/site-packages/django/db/migrations/loader.py", line 209, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/usr/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 65, in applied_migrations
    self.ensure_schema()
  File "/usr/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 52, in ensure_schema
    if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
  File "/usr/lib/python2.7/site-packages/django/db/backends/base/base.py", line 254, in cursor
    return self._cursor()
  File "/usr/lib/python2.7/site-packages/django/db/backends/base/base.py", line 229, in _cursor
    self.ensure_connection()
  File "/usr/lib/python2.7/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
  File "/usr/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/lib/python2.7/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
  File "/usr/lib/python2.7/site-packages/django/db/backends/base/base.py", line 189, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/lib/python2.7/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/usr/lib64/python2.7/site-packages/psycopg2/__init__.py", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: Connection refused
        Is the server running on host "127.0.0.1" and accepting
        TCP/IP connections on port 5432?