[英]django.db.utils.OperationalError Could not connect to server
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',
}
}
It can be some issues:这可能是一些问题:
service postgresql status
service postgresql status
检查它Your PostgresSQl is not running on port 5432. You can check it typing sudo netstat -nl | grep postgres
您的 PostgresSQl 未在端口 5432 上运行。您可以输入
sudo netstat -nl | grep postgres
来检查它。 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.
检查它们是否是 postgres 要求您连接它的内容,以及您要访问的 db_name。
Problems with postmaster.pid in postgres. postgres 中的 postmaster.pid 问题。 It can happen because of a shutdown unproperly done.
这可能是由于关机不当造成的。 It makes to remind a pid alive that doesn't allow your server start.
它可以提醒一个不允许您的服务器启动的 pid 活着。 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在此之后,如果您制作 postgres 的 runserver,它应该可以正常运行
Help in Mac OSX: How to start PostgreSQL server on Mac OS X? Mac OSX 中的帮助: 如何在 Mac OS X 上启动 PostgreSQL 服务器?
Try killing all postgres processes.尝试杀死所有 postgres 进程。 Im on a MAC and this solution that I've found on ubuntus forum really works.
我在 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
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 项目。
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.就我而言,一切都设置得很好,Postgres 有正确的端口,PostgreSQL 运行正常,但是 5432 端口与 phppgadmin 共享,我可以访问 phppgadmin,它让我可以访问 Postgres 数据库服务器,但是我的 Django 应用程序不工作它会返回连接被拒绝错误。 so I changed the port number on the phppgadmin config file (/etc/phppgadmin/config.inc.php) to 5433 from 5432 and all worked fine.
所以我将 phppgadmin 配置文件 (/etc/phppgadmin/config.inc.php) 上的端口号从 5432 更改为 5433,一切正常。
In MacOS I stopped and restarted postgresql according to the advice given in this StackExchange answer: https://dba.stackexchange.com/a/171580/182403在 MacOS 中,我根据 StackExchange 答案中给出的建议停止并重新启动了 postgresql: https ://dba.stackexchange.com/a/171580/182403
brew services stop postgresql
rm /usr/local/var/postgres/postmaster.pid # adjust path accordingly to your install
brew services start postgresql
The following command works for me (Windows)-以下命令适用于我(Windows)-
pg_ctl -D "C:\Program Files\PostgreSQL\11\data" restart
Then run server again-然后再次运行服务器-
python manage.py runserver
重新启动服务器解决了我这边的问题。
In case you are seeing this error while using PostgresSQL from Google Cloud follow all configurations as mentioned in https://cloud.google.com/python/django/flexible-environment#macos-64-bit如果您在使用 Google Cloud 中的 PostgresSQL 时看到此错误,请遵循https://cloud.google.com/python/django/flexible-environment#macos-64-bit中提到的所有配置
Also separate the HOST configuration as below还将 HOST 配置分开如下
DATABASES['default']['HOST'] = '/cloudsql/' if os.getenv('GAE_INSTANCE'): pass else: DATABASES['default']['HOST'] = '127.0.0.1' DATABASES['default']['HOST'] = '/cloudsql/' if os.getenv('GAE_INSTANCE'): pass else: DATABASES['default']['HOST'] = '127.0.0.1'
This helps in resolving this error.这有助于解决此错误。
DATABASE
- HOST
settings you should use local IP (127.0.0.1) not your public IP.DATABASE
- HOST
设置下的project_folder/settings.py中,您应该使用本地 IP (127.0.0.1) 而不是您的公共 IP。DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'yourdb',
'USER': 'youruser',
'PASSWORD': 'yourpass',
'HOST': '127.0.0.1',
'PORT': '5432'
}
}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'yourdb',
'USER': 'youruser',
'PASSWORD': 'yourpass',
'HOST': '188.252.196.234 ',
'PORT': '5432'
}
}
I had the same issue.我遇到过同样的问题。 This error occurs when an improper system shutdown has been done.
当系统不正确关闭时会发生此错误。 I'm on an M1, and I've installed PostgreSQL via Homebrew.
我在 M1 上,我已经通过 Homebrew 安装了 PostgreSQL。
I tried to start PostgreSQL after receiving an error identical to yours using brew services start postgresql
, but I got this error:在使用
brew services start postgresql
收到与您相同的错误后,我尝试启动 PostgreSQL,但出现此错误:
Bootstrap failed: 5: Input/output error
Try re-running the command as root for richer errors.
Error: Failure while executing; `/bin/launchctl bootstrap gui/501 /Users/adithraghav/Library/LaunchAgents/homebrew.mxcl.postgresql.plist` exited with 5.
So, I tried to stop the running instance and start it again with brew services restart postgresql
.因此,我尝试停止正在运行的实例并使用
brew services restart postgresql
启动它。
This line gave me the following output:这一行给了我以下输出:
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
I checked the port on which PostgreSQL was running with sudo netstat -nl | grep postgres
我使用
sudo netstat -nl | grep postgres
检查了运行 PostgreSQL 的端口。 sudo netstat -nl | grep postgres
, but received no output. sudo netstat -nl | grep postgres
,但没有收到任何输出。
So I deleted the existing postmaster.pid
file with this:所以我删除了现有的
postmaster.pid
文件:
rm /opt/homebrew/var/postgres/postmaster.pid
NOTE: If you are not on an M1 Mac, you have to run rm /usr/local/var/postgres/postmaster.pid
.注意:如果您不在 M1 Mac 上,则必须运行
rm /usr/local/var/postgres/postmaster.pid
。
Then, I ran pg_resetwal -f /opt/homebrew/var/postgres
to reset the write-ahead log.然后,我运行
pg_resetwal -f /opt/homebrew/var/postgres
来重置预写日志。 (NOTE: From Postgres 10 and onwards, pg_resetxlog
has been renamed to pg_resetwal
). (注意:从 Postgres 10 起,
pg_resetxlog
已重命名为pg_resetwal
)。
Now, python3 manage.py runserver
works with no issues :)现在,
python3 manage.py runserver
可以正常工作了 :)
转到 aws 实例 -> 安全组 -> 源 -> 入站 -> ::0
You might not connecting to the right database.您可能没有连接到正确的数据库。 If you are using Docker be sure you are using 5432 as port at the outside of the image.
如果您使用 Docker,请确保您使用 5432 作为镜像外部的端口。
eg:例如:
db: image: postgres:12.0-alpine volumes: - postgres_data:/var/lib/postgresql/data/ env_file: - ./postgres/.env ports: - 5432 :5432 db:图像:postgres:12.0-alpine 卷:- postgres_data:/var/lib/postgresql/data/env_file:- ./postgres/.env 端口:- 5432 :5432
One problem that I found common with Django/Postgres (especially with Docker) is that your Django Web App maybe starting up before your Postgres server starts up.我发现 Django/Postgres(尤其是 Docker)常见的一个问题是您的 Django Web 应用程序可能在您的 Postgres 服务器启动之前启动。 If the other solutions don't work, try to restart your Django App after start up.
如果其他解决方案不起作用,请在启动后尝试重新启动您的 Django App。 With Docker, the command would be
docker restart <web-app container name>
使用 Docker,命令将是
docker restart <web-app container name>
You have to enable listen addresses if you are using remote database.如果您使用远程数据库,则必须启用监听地址。
cd /etc/postgresql/12.x/main/
open file named postgresql.conf打开名为 postgresql.conf 的文件
sudo nano postgresql.conf
add this line to that file将此行添加到该文件
listen_addresses = '*'
then an open a file named pg_hba.conf然后打开一个名为 pg_hba.conf 的文件
sudo nano pg_hba.conf
and add this line to that file并将这一行添加到该文件
host all all 0.0.0.0/0 md5
restart your server重启你的服务器
sudo /etc/init.d/postgresql restart
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.