[英]PostgreSQL Connection Timed Out OperationalError on Django and new server
After my server got compromised, I've had to migrate to a new droplet - both on DigitalOcean, Debian Jessie, so I shouldn't have the Heroku issues that a lot of the similar questions seem to cover. 服务器受损后,我不得不迁移到新的Droplet-在DigitalOcean和Debian Jessie上,所以我不应该遇到许多类似问题似乎都涉及到的Heroku问题。
I copied the database over using pg_dump, and it seems intact. 我使用pg_dump复制了数据库,它似乎完好无损。 PostgreSQL is installed, and running, but whenever I try to utilise the database from the Django ORM, I get the error -
PostgreSQL已安装并正在运行,但是每当我尝试使用Django ORM中的数据库时,都会收到错误消息-
OperationalError: could not connect to server: Connection timed out
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
So, some obvious things first. 因此,首先是一些显而易见的事情。
Netstat shows: Netstat显示:
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 613/postgres
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 554/redis-server 12
I can see several PostgreSQL process in top
and using ps auxw | grep postgres
我可以看到
top
和使用ps auxw | grep postgres
几个PostgreSQL进程。 ps auxw | grep postgres
: ps auxw | grep postgres
:
1072 postgres 20 0 234680 129672 126676 S 6.0 3.2 3:06.98 postgres: me databasename [local] COPY +
640 postgres 20 0 227000 119564 117876 S 0.3 2.9 0:03.77 postgres: checkpointer process +
613 postgres 20 0 226712 21252 19824 S 0.0 0.5 0:00.05 /usr/lib/postgresql/9.4/bin/postgres -D /var/lib/postgresql/9.4/main -c config_f+
641 postgres 20 0 226712 5556 4080 S 0.0 0.1 0:02.59 postgres: writer process
systemctl
shows systemctl
显示
postgresql.service loaded active exited
postgresql@9.4-main.service loaded active running
psycopg2 is installed, and I'm working from inside the virtualenv. psycopg2已安装,我正在virtualenv内部进行工作。
Any queries I run from shell using the Django ORM return the above error. 我使用Django ORM从shell运行的任何查询均返回上述错误。 I can access and run queries from psql, and it returns data fine, so PostgreSQL appears to be running, but Django can't reach it for whatever reason.
我可以从psql访问和运行查询,并且它返回的数据很好,因此PostgreSQL似乎正在运行,但是Django由于任何原因都无法访问它。 The password and username are correct, although I think that would throw a different error in anycase.
密码和用户名是正确的,尽管我认为这在任何情况下都会引发不同的错误。 Restarting the service it makes no difference.
重新启动服务没有什么区别。
My Django settings are: 我的Django设置是:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": "databasename",
"USER": "databaseusername",
"PASSWORD": "databasepasswordhere",
"HOST": "localhost",
"PORT": "5432",
"CONN_MAX_AGE":360,
}
}
(I've tried both localhost and 127.0.0.1 as host, in case it was an IPv4/IPv6 resolution thing, albeit unlikely) pip freeze
shows: (我尝试将localhost和127.0.0.1都用作主机,以防它是IPv4 / IPv6分辨率的东西,尽管不太可能)
pip freeze
显示:
Django==1.9.6
argparse==1.2.1
click==6.6
django-extensions==1.6.7
django-redis==4.4.3
django-redis-cache==1.6.5
django-rq==0.9.1
django-rq-dashboard==0.3.0
gunicorn==19.5.0
hiredis==0.2.0
newrelic==2.64.0.48
nltk==3.2.1
oauthlib==1.1.1
psycopg2==2.6.1
requests==2.10.0
requests-oauthlib==0.6.1
rq==0.6.0
six==1.10.0
tweepy==3.5.0
wsgiref==0.1.2
Per this question, I've checked that PostgreSQL, PostgreSQL-contrib, libpq-dev Python-dev are installed. 对于这个问题,我检查了是否安装了PostgreSQL,PostgreSQL-contrib,libpq-dev Python-dev。
Testing the connection on command-line as per this question throws the same error. 按照此问题在命令行上测试连接会引发相同的错误。
The real kicker here is - this is all working on the old server, and I can run ORM queries just fine - but it's not a secure server anymore, package manager is broken, so I need to abandon it, and need the new one to work. 真正的起因是-所有这些都可以在旧服务器上运行,并且我可以很好地运行ORM查询-但它现在不再是安全服务器,包管理器已损坏,因此我需要放弃它,而需要新的工作。 I can't see any differences in configuration between the two - I wondered if there was a permissions issue, but I get the same issue running as root/via sudo as my regular user.
我看不到两者之间的配置存在任何差异-我想知道是否存在权限问题,但是我以普通用户身份通过root / sudo运行相同的问题。
I'm sure there's a simple configuration setting I've missed, but I'm pretty stumped at the moment! 我敢肯定我错过了一个简单的配置设置,但此刻我很沮丧!
Edit: 编辑:
Grabbing the PID of postgres main process and running lsof gives this, though: 捕获postgres主进程的PID并运行lsof可以得到以下效果:
postgres 613 postgres 6u IPv4 11589 0t0 TCP localhost:postgresql (LISTEN) postgres 613 postgres 7u unix 0xffff8800d8e1cc00 0t0 11591 /var/run/postgresql/.s.PGSQL.5432
Which seems weird. 这看起来很奇怪。 Is it defaulting to a Unix socket there?
是否默认使用Unix套接字?
A connection timeout error suggests that the database server is not accessible with your settings over the network in a way that does not return an immediate error. 连接超时错误表明数据库设置无法通过网络访问数据库服务器,而不会立即返回错误。 The best approach, in my experience, is to troubleshoot the two sides of the connection separately.
根据我的经验,最好的方法是分别对连接的两侧进行故障排除。 This means testing the server-side of the connection from the psql command line first, and then testing the django side once that is resolved.
这意味着首先要从psql命令行测试连接的服务器端,然后在解决后再测试django端。
I assume here you can install psql.on the server. 我假设您可以在服务器上安装psql.。
Immediate checks: 立即检查:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.