When I connect Postgres
database with Django
, it gives me following error (screenshot attached either). 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:
'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.sstatic.net/fqrZQ.png
Sure, you can go to the code and place the pdb
before the self.connection = self.get_new_connection(conn_params)
line, something like:
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) 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.