简体   繁体   English

Django和新服务器上的PostgreSQL连接超时OperationalError

[英]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: 立即检查:

  1. Can you ping the server from the server? 您可以从服务器ping服务器吗? If not, start there in troubleshooting the network. 如果不是,请从那里开始对网络进行故障排除。 (I see this is localhost but still try -- it takes very little time and at least verifies assumptions). (我看到这是本地主机,但仍然可以尝试-它花费很少的时间,并且至少可以验证假设)。
  2. Can you connect to the server via psql using the same port, ip address, etc? 您可以使用相同的端口,IP地址等通过psql连接到服务器吗? If not correct errors you get while trying that, try to fix those first. 如果尝试此操作时遇到的错误不正确,请尝试首先修复这些错误。 Chances are the same problems will occur here too. 同样,这里也会发生同样的问题。 You may need to check firewalling and postgresql configuration if this times out. 如果超时,您可能需要检查防火墙和postgresql配置。
  3. Once these are working, then start trying from django again. 一旦这些工作,然后再从Django开始尝试。 Then you know it is Django/Python specific and no server-side. 然后,您知道它是特定于Django / Python的,并且没有服务器端。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 kubernetes 上的 Postgres+django:django.db.utils.OperationalError:无法连接到服务器:连接超时 - Postgres+django on kubernetes: django.db.utils.OperationalError: could not connect to server: Connection timed out Django 与 Docker 上的 postgreSQL DB - django.db.utils.OperationalError:无法连接到服务器:连接被拒绝 - Django with postgreSQL DB on Docker - django.db.utils.OperationalError: could not connect to server: Connection refused POST>连接超时Django - POST > Connection timed out Django PostgreSQL-django.db.utils.OperationalError:无法连接到服务器:连接被拒绝 - postgresql - django.db.utils.OperationalError: could not connect to server: Connection refused Django - App Engine - 云 SQL (PostgreSQL) - 操作错误:无法连接到服务器:连接被拒绝 - Django - App Engine - Cloud SQL (PostgreSQL) - OperationalError: could not connect to server: Connection refused Django OperationalError:无法为连接分叉新进程 - Django OperationalError: could not fork new process for connection 使用theano时Django服务器超时 - Django Server timed out when working with theano Django OperationalError(2013年,“在查询期间丢失了与MySQL服务器的连接”) - Django OperationalError(2013, “Lost connection to MySQL server during query”) django.db.utils.OperationalError:无法连接到服务器:连接被拒绝 - django.db.utils.OperationalError: could not connect to server: Connection refused OperationalError:无法连接到服务器:操作超时-尝试将数据库从sqlite更改为postresql时 - OperationalError: could not connect to server: Operation timed out - when trying to change database from sqlite to postresql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM