OperationalError:无法连接到服务器:连接被拒绝 - 尝试连接到新的postgresql数据库时

时间:2018-05-14 09:57:05

标签: postgresql

我将我的开发Django数据库从sqlite更改为postgresql。

我已将设置更改为适当的值:

DATABASES = {
    'default': {
        'ENGINE': "django.db.backends.postgresql_psycopg2",
        'NAME': "db.postr",
        'USER': "zorgan",
        'PASSWORD': config('DB_PASSWORD'),
        'HOST': "localhost",
        'PORT': '',
    }
}

然而,当我执行python manage.py makemigrations时,我收到此错误:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/core/management/commands/makemigrations.py", line 110, in handle
    loader.check_consistent_history(connection)
  File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/db/migrations/loader.py", line 282, in check_consistent_history
    applied = recorder.applied_migrations()
  File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/db/migrations/recorder.py", line 65, in applied_migrations
    self.ensure_schema()
  File "/Users/zorgan/Desktop/postr1/lib/python3.5/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 "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/db/backends/base/base.py", line 254, in cursor
    return self._cursor()
  File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/db/backends/base/base.py", line 229, in _cursor
    self.ensure_connection()
  File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
  File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
  File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/db/backends/base/base.py", line 189, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/Users/zorgan/Desktop/postr1/lib/python3.5/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 "localhost" (::1) and accepting
        TCP/IP connections on port 5432?
could not connect to server: Connection refused
        Is the server running on host "localhost" (fe80::1) and accepting
        TCP/IP connections on port 5432?
could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?

我认为问题是我没有Postgres数据库,但是当我在开发环境中执行sudo -u postgres psql时,它会返回:

sudo: unknown user: postgres
sudo: unable to initialize policy plugin

这就是我要做的事情to create a postgres database

有什么建议吗?

0 个答案:

没有答案