I'm using the django rest framework and trying to connect a postgresql database. If I have the connection to the database written explicitly in the settings.py
file, running python manage.py runserver
works fine, but as soon as I replace the database connection with the dotenv values, the connection breaks. How do I fix this?
example
This works fine
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'ticketdb',
'USER': 'postgres',
'PASSWORD': 'pretendmyrealpasswordishere',
'HOST': 'localhost',
}
}
This does not and throws this error.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('DB_NAME'),
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_USER_PASSWORD'),
'HOST': os.environ.get('DB_HOST'),
'PORT': os.environ.get('DB_PORT'),
}
}
Traceback
Traceback (most recent call last):
File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
self.run()
File "/usr/lib/python3.10/threading.py", line 953, in run
self._target(*self._args, **self._kwargs)
File "/home/mikha/.local/share/virtualenvs/django-tracker-Ko6xeuHD/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "/home/mikha/.local/share/virtualenvs/django-tracker-Ko6xeuHD/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 137, in inner_run
self.check_migrations()
File "/home/mikha/.local/share/virtualenvs/django-tracker-Ko6xeuHD/lib/python3.10/site-packages/django/core/management/base.py", line 564, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "/home/mikha/.local/share/virtualenvs/django-tracker-Ko6xeuHD/lib/python3.10/site-packages/django/db/migrations/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "/home/mikha/.local/share/virtualenvs/django-tracker-Ko6xeuHD/lib/python3.10/site-packages/django/db/migrations/loader.py", line 58, in __init__
self.build_graph()
File "/home/mikha/.local/share/virtualenvs/django-tracker-Ko6xeuHD/lib/python3.10/site-packages/django/db/migrations/loader.py", line 235, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/home/mikha/.local/share/virtualenvs/django-tracker-Ko6xeuHD/lib/python3.10/site-packages/django/db/migrations/recorder.py", line 81, in applied_migrations
if self.has_table():
File "/home/mikha/.local/share/virtualenvs/django-tracker-Ko6xeuHD/lib/python3.10/site-packages/django/db/migrations/recorder.py", line 57, in has_table
with self.connection.cursor() as cursor:
File "/home/mikha/.local/share/virtualenvs/django-tracker-Ko6xeuHD/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/home/mikha/.local/share/virtualenvs/django-tracker-Ko6xeuHD/lib/python3.10/site-packages/django/db/backends/base/base.py", line 323, in cursor
return self._cursor()
File "/home/mikha/.local/share/virtualenvs/django-tracker-Ko6xeuHD/lib/python3.10/site-packages/django/db/backends/base/base.py", line 299, in _cursor
self.ensure_connection()
File "/home/mikha/.local/share/virtualenvs/django-tracker-Ko6xeuHD/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/home/mikha/.local/share/virtualenvs/django-tracker-Ko6xeuHD/lib/python3.10/site-packages/django/db/backends/base/base.py", line 281, in ensure_connection
with self.wrap_database_errors:
File "/home/mikha/.local/share/virtualenvs/django-tracker-Ko6xeuHD/lib/python3.10/site-packages/django/db/utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/mikha/.local/share/virtualenvs/django-tracker-Ko6xeuHD/lib/python3.10/site-packages/django/db/backends/base/base.py", line 282, in ensure_connection
self.connect()
File "/home/mikha/.local/share/virtualenvs/django-tracker-Ko6xeuHD/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/home/mikha/.local/share/virtualenvs/django-tracker-Ko6xeuHD/lib/python3.10/site-packages/django/db/backends/base/base.py", line 263, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/mikha/.local/share/virtualenvs/django-tracker-Ko6xeuHD/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/home/mikha/.local/share/virtualenvs/django-tracker-Ko6xeuHD/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 215, in get_new_connection
connection = Database.connect(**conn_params)
File "/home/mikha/.local/share/virtualenvs/django-tracker-Ko6xeuHD/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?
CodePudding user response:
- Option 1:
python-decouple
package
pip install python-decouple
In your settings.py file:
from decouple import config
Access your environment variables as:
NAME = config("DB_NAME")
Official docs: https://github.com/henriquebastos/python-decouple/
- Option 2:
python-dotenv
package
pip install python-dotenv
In your settings.py file:
from dotenv import load_dotenv
load_dotenv()
Access your environment variables as:
NAME = os.getenv("DB_NAME")
Official docs: https://saurabh-kumar.com/python-dotenv/