Python django.db.utils.OperationalError 无法连接到服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29937378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
django.db.utils.OperationalError Could not connect to server
提问by Liondancer
I am not sure how to fix this issue
我不知道如何解决这个问题
I have no idea why I am getting this error when I try to runserver
:
我不知道为什么我在尝试时会收到此错误runserver
:
Performing system checks...
System check identified no issues (0 silenced).
Unhandled exception in thread started by <function wrapper at 0x1085589b0>
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 222, in wrapper
fn(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 107, in inner_run
self.check_migrations()
File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 159, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 17, in __init__
self.loader = MigrationLoader(self.connection)
File "/Library/Python/2.7/site-packages/django/db/migrations/loader.py", line 49, in __init__
self.build_graph()
File "/Library/Python/2.7/site-packages/django/db/migrations/loader.py", line 184, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/Library/Python/2.7/site-packages/django/db/migrations/recorder.py", line 59, in applied_migrations
self.ensure_schema()
File "/Library/Python/2.7/site-packages/django/db/migrations/recorder.py", line 49, in ensure_schema
if self.Migration._meta.db_table in self.connection.introspection.get_table_list(self.connection.cursor()):
File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 165, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 138, in _cursor
self.ensure_connection()
File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 133, in ensure_connection
self.connect()
File "/Library/Python/2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 133, in ensure_connection
self.connect()
File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 122, in connect
self.connection = self.get_new_connection(conn_params)
File "/Library/Python/2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 134, in get_new_connection
return Database.connect(**conn_params)
File "/Library/Python/2.7/site-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
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?
When I try to connect to postgres:
当我尝试连接到 postgres 时:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
settings.py:
设置.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'beerad',
'USER': 'bli1',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
采纳答案by Ricardo Burillo
It can be some issues:
可能是一些问题:
- PostgreSQL is not running. Check it with sudo
service postgresql status
Your PostgresSQl is not running on port 5432. You can check it typing
sudo netstat -nl | grep postgres
You have something wrong trying to connect to your db like the username, the password or the databasename. Check that they are what postgres ask for you to connect it and that is the db_name that you want to access to.
Problems with postmaster.pid in postgres. It can happen because of a shutdown unproperly done. It makes to remind a pid alive that doesn't allow your server start. To fix it you have to:
* rm /usr/local/var/postgres/postmaster.pid * pg_resetxlog -f /usr/local/var/postgres
After this it should run properly if you make the runserver of postgres
- PostgreSQL 没有运行。用 sudo 检查
service postgresql status
你的 PostgresSQl 没有在端口 5432 上运行。你可以检查它输入
sudo netstat -nl | grep postgres
您尝试连接到您的数据库时出现错误,例如用户名、密码或数据库名称。检查它们是否是 postgres 要求您连接它的内容,以及您要访问的 db_name。
postgres 中 postmaster.pid 的问题。它可能是由于关闭不当而发生的。它会提醒一个不允许您的服务器启动的 pid 活动。要修复它,您必须:
* rm /usr/local/var/postgres/postmaster.pid * pg_resetxlog -f /usr/local/var/postgres
在此之后,如果您制作 postgres 的运行服务器,它应该可以正常运行
Help in Mac OSX: How to start PostgreSQL server on Mac OS X?
Mac OSX 中的帮助:如何在 Mac OS X 上启动 PostgreSQL 服务器?
回答by Jcc.Sanabria
Try killing all postgres processes. Im on a MAC and this solution that I've found on ubuntus forum really works.
尝试杀死所有 postgres 进程。我在 MAC 上,我在 ubuntus 论坛上找到的这个解决方案确实有效。
https://askubuntu.com/questions/547434/how-to-nicely-stop-all-postgres-processes
https://askubuntu.com/questions/547434/how-to-nicely-stop-all-postgres-processes
回答by Abdul Majeed
For Windows
对于 Windows
Go to search bar and just write "Open psql
" and hit Enter.
转到搜索栏,只需Open psql
输入“ ”,然后按 Enter。
Once screen is opened, rerun django project.
打开屏幕后,重新运行 django 项目。
回答by Aldo Okware
In my case, all was set up well and Postgres had the right port, PostgreSQL was running normally, but the 5432 port was being shared with phppgadmin, I could access the phppgadmin that gives me web access to Postgres database server, but my Django application was not working it would return Connection refused error. so I changed the port number on the phppgadmin config file (/etc/phppgadmin/config.inc.php) to 5433 from 5432 and all worked fine.
就我而言,一切都设置得很好,Postgres 有正确的端口,PostgreSQL 正常运行,但 5432 端口与 phppgadmin 共享,我可以访问 phppgadmin,它使我可以访问 Postgres 数据库服务器,但我的 Django 应用程序不工作它会返回连接拒绝错误。所以我将 phppgadmin 配置文件 (/etc/phppgadmin/config.inc.php) 上的端口号从 5432 更改为 5433 并且一切正常。
回答by Ankush Sahu
Go to aws instance -> security groups -> source -> inbound -> ::0
转到 aws 实例 -> 安全组 -> 源 -> 入站 -> ::0
回答by Leena Bhandari
The following command works for me (Windows)-
以下命令适用于我(Windows)-
pg_ctl -D "C:\Program Files\PostgreSQL\data" restart
Then run server again-
然后再次运行服务器-
python manage.py runserver