简体   繁体   English

django.db.utils.OperationalError:无法连接到服务器:Unix 域套接字“/tmp/.s.PGSQL.5432”上没有此类文件?

[英]django.db.utils.OperationalError: could not connect to server: No such file on Unix domain socket “/tmp/.s.PGSQL.5432”?

When I connect Postgres database with Django , it gives me following error (screenshot attached either).当我将Postgres数据库与Django连接时,它给了我以下错误(也附有屏幕截图)。 When I use direct database name or user etc then this work fine but when i use through environment variable, it is not working.当我使用直接数据库名称或用户等时,这可以正常工作,但是当我通过环境变量使用时,它不起作用。

this is my setting.py file:这是我的 setting.py 文件:

'ENGINE': 'django.db.backends.postgresql_psycopg2',

        'NAME': os.environ.get('DATABASE_NAME'),
        'USER': os.environ.get('DATABASE_USER'),
        'PASSWORD': os.environ.get('DATABASE_PASSWORD'),
        'HOST': os.environ.get('DATABASE_HOST'),
        'PORT': 5432
    this is my .bash_profile enviroment variable
    
     export DATABASE_NAME='blog'
     export DATABASE_USER='bloguser'
     export DATABASE_PASSWORD='blog@123'
     export DATABASE_HOST='localhost'

The error itself:错误本身:

    self.connection = self.get_new_connection(conn_params)
  File "/home/kamran/anaconda3/envs/DjangoEnv/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 178, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/home/kamran/anaconda3/envs/DjangoEnv/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: 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"?

And the attached image: https://i.stack.imgur.com/fqrZQ.png并附上图片: https://i.stack.imgur.com/fqrZQ.png

PostgreSQL is not running or it's not configured properly. PostgreSQL 未运行或未正确配置。

Change listen_address localhost to * in file /etc/postgresql/{version_number}/main/postgresql.conf将文件/etc/postgresql/{version_number}/main/postgresql.conf中的 listen_address localhost to *

listen_addresses = '*'

In pg_hba.conf , add the following line in # IPv4 local connections :pg_hba.conf中,在# IPv4 local connections中添加以下行:

host    all             all             0.0.0.0/0               md5

Sure, you can go to the code and place the pdb before the self.connection = self.get_new_connection(conn_params) line, something like:当然,您可以将 go 放入代码并将pdb放在self.connection = self.get_new_connection(conn_params)行之前,例如:

import pdb; pdb.set_trace()
self.connection = self.get_new_connection(conn_params)

Run the server and once it reaches that pdb line you could interact with the code in runtime mode, it will show you a pdb console and then you can see if that environment variables are setted or not:运行服务器,一旦它到达该pdb行,您可以在运行时模式下与代码交互,它会显示一个pdb控制台,然后您可以查看是否设置了该环境变量:

(pdb) p os.environ.get('DATABASE_NAME')
(pdb) p os.environ.get('DATABASE_USER')
...

It will allow you to see if the variables are setted and with the correct value.它将允许您查看变量是否已设置以及是否具有正确的值。

暂无
暂无

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

相关问题 django.db.utils.OperationalError:无法连接到服务器:没有这样的文件或目录 - django.db.utils.OperationalError: could not connect to server: No such file or directory Django、docker、PostGIS 中的错误:服务器是否在本地运行并接受 Unix 域套接字“/var/run/postgresql/.s.PGSQL”上的连接? - Error in Django,docker,PostGIS: Is the server running locally and accepting connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”? django.db.utils.OperationalError 无法连接到服务器 - django.db.utils.OperationalError Could not connect to server django.db.utils.OperationalError:无法连接到服务器:连接被拒绝 - django.db.utils.OperationalError: could not connect to server: Connection refused 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 Docker 和 Django。 django.db.utils.OperationalError: 无法连接到服务器 - Docker and Django. django.db.utils.OperationalError: could not connect to server kubernetes 上的 Postgres+django:django.db.utils.OperationalError:无法连接到服务器:连接超时 - Postgres+django on kubernetes: django.db.utils.OperationalError: could not connect to server: Connection timed out 在 Dockerfile 中运行“manage.py compilemessages”给出“django.db.utils.OperationalError:无法连接到服务器:没有这样的文件或目录” - Running “manage.py compilemessages” in Dockerfile gives “django.db.utils.OperationalError: could not connect to server: No such file or directory” Postgres django.db.utils.OperationalError:无法连接到服务器:连接被拒绝 - Postgres django.db.utils.OperationalError: could not connect to server: Connection refused PostgreSQL-django.db.utils.OperationalError:无法连接到服务器:连接被拒绝 - postgresql - django.db.utils.OperationalError: could not connect to server: Connection refused
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM