服务器是不是在主机“localhost”(127.0.0.1) 上运行并接受端口 5432 上的 TCP/IP 连接?

Posted

技术标签:

【中文标题】服务器是不是在主机“localhost”(127.0.0.1) 上运行并接受端口 5432 上的 TCP/IP 连接?【英文标题】:Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?服务器是否在主机“localhost”(127.0.0.1) 上运行并接受端口 5432 上的 TCP/IP 连接? 【发布时间】:2018-12-17 03:14:43 【问题描述】:

我尝试在 Heroku 上部署我的 Django 项目,但每次我尝试“heroku run python3 manage.py migrate”时都会收到此错误:

/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
  """)
Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
    self.connect()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 168, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: 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?


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

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 79, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/loader.py", line 49, in __init__
    self.build_graph()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/loader.py", line 206, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 61, in applied_migrations
    if self.has_table():
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 44, in has_table
    return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 255, in cursor
    return self._cursor()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 232, in _cursor
    self.ensure_connection()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
    self.connect()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
    self.connect()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 168, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/app/.heroku/python/lib/python3.6/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" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?

我已经研究并尝试了很多解决方案,但没有任何效果。我在 postgres.conf 文件中将 localhost 更改为 *,我在 pg_hba.conf 中将 127.0.0.1/32 更改为 0.0.0.0/0,我重新启动了 Postgres 数千次,我确定它正在运行。我正在使用 Linux 和虚拟环境。还有什么选择吗?

【问题讨论】:

你本地的 pg_hba.conf 在这里完全不相关,因为这是在 Heroku 上运行的。你的 Django 数据库设置在哪里?你在使用 dj_database_url 吗? 【参考方案1】:

好的,我找到了解决方案。我需要将其添加到我的 settings.py 文件中:

from decouple import config
DATABASES = 
    'default': dj_database_url.config(
        default=config('DATABASE_URL')
    )

【讨论】:

DATABASE_URL 是 RDS 端点??那么它是一个弹性豆茎变量吗?? 你如何添加它?因为我们已经有了默认的,我们应该替换它

以上是关于服务器是不是在主机“localhost”(127.0.0.1) 上运行并接受端口 5432 上的 TCP/IP 连接?的主要内容,如果未能解决你的问题,请参考以下文章

“本地主机”与“127.0.0.1”性能

localhost127.0.0.1本机IP虚拟主机kotlin

谈谈localhost与127.0.0.1

localhost与127.0.0.1 本机IP

毕竟堆栈溢出解决方案仍然无法在我的本地主机服务器上登录

win10下关于apache配置虚拟主机