无法连接到服务器

无法连接到服务器

本文介绍了无法连接到服务器:连接被拒绝 (0x0000274D/10061) - 远程服务器上的 PostgreSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去 2 天,我一直在尝试使用远程服务器 (Ubuntu) 和 Django 项目来设置新的 postgresql 数据库,但一遍又一遍地遇到同样的问题.我该怎么办?

I've been trying to set up a new postgresql database with a remote server (Ubuntu) a Django project for the last 2 days but keep having this same issue over and over again. What should I do?

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

Here's the error output that I'm getting when I try to make migrations to the database:

$ 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?

以及我的设置文件中的代码:

And the code in my settings file:

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

我已经运行了以下但仍然收到错误:

I've run the following to but still getting the error:

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 相同,但仍然如此.

And I also created a new Server in PgAdmin with the same Ip Adress as my Droplet on Digital Ocean but still.

如果你有任何东西,请告诉我,提前谢谢!

Please let me know if you have anything, thanks in advance!

推荐答案

好的,我找到了解决方案:

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

I replaced this code in my settings.py file:

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:

Installed dj-database-url with pip:

pip install dj_database_url

现在它完美地工作了!

这篇关于无法连接到服务器:连接被拒绝 (0x0000274D/10061) - 远程服务器上的 PostgreSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 06:55