无法连接到服务器:连接被拒绝(0x0000274D / 10061)-远程服务器上的PostgreSQL

时间:2019-04-21 03:04:06

标签: django postgresql ubuntu-18.04

在过去的两天里,我一直在尝试使用远程服务器(Ubuntu)和Django项目建立一个新的postgresql数据库,但一遍又一遍都遇到同样的问题。我该怎么办?

这是我尝试迁移到数据库时得到的错误输出:

$ python manage.py makemigrations
Traceback (most recent call last):
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection
    self.connect()
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\backends\base\base.py", line 195, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\backends\postgresql\base.py", line 178, in get_new_connection
    connection = Database.connect(**conn_params)
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\psycopg2\__init__.py", line 126, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused (0x0000274D/10061)
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\core\management\base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\core\management\commands\makemigrations.py", line 101, in handle
    loader.check_consistent_history(connection)
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\migrations\loader.py", line 283, in check_consistent_history
    applied = recorder.applied_migrations()
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\migrations\recorder.py", line 73, in applied_migrations
    if self.has_table():
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\migrations\recorder.py", line 56, in has_table
    return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\backends\base\base.py", line 256, in cursor
    return self._cursor()
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\backends\base\base.py", line 233, in _cursor
    self.ensure_connection()
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection
    self.connect()
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection
    self.connect()
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\backends\base\base.py", line 195, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\backends\postgresql\base.py", line 178, in get_new_connection
    connection = Database.connect(**conn_params)
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\psycopg2\__init__.py", line 126, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: Connection
refused (0x0000274D/10061)
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?

还有我的设置文件中的代码:

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

我已经执行以下操作,但是仍然出现错误:

sudo -i -u root
echo "listen_addresses = '*'" >> /etc/postgresql/*/main/postgresql.conf
echo 'host all all 0.0.0.0/0 md5' >> /etc/postgresql/*/main/pg_hba.conf
sudo /etc/init.d/postgresql restart

我还在PgAdmin中创建了一个新服务器,其IP地址与我在Digital Ocean上的Droplet相同。

请让我知道是否有任何东西,谢谢!!

2 个答案:

答案 0 :(得分:0)

Postgres使用PORT 5432 作为标准端口。 我建议以这种方式更新您的settings.py:

   DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.postgresql',
          'NAME': 'dbname',
          'USER': 'postgres',
          'PASSWORD': 'password',
          'HOST': 'localhost',
          'PORT': '5432',
           }

     }

答案 1 :(得分:0)

好的,我找到了解决方法

我在我的settings.py文件中替换了此代码:

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

与此:

DATABASES = {'default': dj_database_url.config(default='postgres://YOURUSER:YOURPASSWORD@YOURHOST:5432/YOURDATABASENAME')}

通过pip安装了dj-database-url:

pip install dj_database_url

现在它可以正常工作了!