[英]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.