settings.DATABASES is improperly configured - django

total newbie here, sorry
Mac OSX 10.8 Python 2.7 (installed with homebrew)
PostgreSQL 9.4(installed with homebrew)
psycopg2 2.5 (installed with macports)
Django 1.0.4 (installed via python setup.py install)
I'm using this tutorial, and after starting python manage.py shell I ran
>>> from django.db import connection
>>> cursor = connection.cursor()
and got the following:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/dummy/base.py", line 15, in complain
raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
The DATABASES section of my settings.py file looks like this:
DATABASE_ENGINE = 'django.db.backends.postgresql_psycopg2' #postgresql_psycopg2
DATABASE_NAME = 'mydatabase' #mydatabase
DATABASE_USER = 'sarahr6' # Not used with sqlite3.
DATABASE_PASSWORD = '' # Not used with sqlite3.
DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3.
So I can't figure out why it says it's improperly configured?

You need to specify DATABASES dictionary in settings.py:
A dictionary containing the settings for all databases to be used with
Django. It is a nested dictionary whose contents maps database aliases
to a dictionary containing the options for an individual database.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydatabase',
'USER': 'sarahr6',
'PASSWORD': '',
'HOST': '',
'PORT': ''
}
}

Related

KeyError: 'DB_NAME' when doing makemigrations in django

I store all my secrets and database params in the dev.env file.
I have 3 different settings files - base, dev and prod.
There is an SQLite database in base, and I want to connect to Postgres in dev.
So I upload my secrets with the environment variable in my dev setting file like this:
from dotenv import load_dotenv
load_dotenv(os.environ.get('ENV_CONFIG', ''))
And I override my database settings in dev settings file:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ['DB_NAME'],
'USER': os.environ['DB_USER'],
'PASSWORD': os.environ['DB_PASS'],
'HOST': os.environ['DB_HOST'],
'PORT': os.environ['DB_PORT'],
}
}
But when I run makemigrations with dev settings file:
./manage.py makemigrations --settings=app.settings.dev
I get an error:
File "/Users/admin/Desktop/Programming/Python/UkranianFunds/src/app/settings/dev.py", line 35, in <module>
'NAME': os.environ['DB_NAME'],
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/os.py", line 679, in __getitem__
raise KeyError(key) from None
KeyError: 'DB_NAME'
I checked and my secret with the key DB_NAME clearly appears in the settings file - I printed it successfully. The name of the database is correct.
What are other reasons that cause that?
Your loaded dev.env file does not contain 'DB_NAME' as a key.
I solved it by replacing os.environ['DB_NAME'] to os.environ.get('DB_NAME').
Weird situation for me because the problem occurred only when I did makemigrations.
After I migrated to Postgres, I tried to run the app with os.environ['DB_NAME'] and it worked fine.
So it seems that the KeyError is being raised only during makemigrations.

how to fix raise ImproperlyConfigured("settings.DATABASES is improperly configured. when deploying on railway

hey guys so i'm follwing this guide
https://dev.to/mr_destructive/django-postgresql-deployment-on-railway-app-d54
on how to deploy my django project on railway
i have everything set locally, it working but once i deploy, the app crashes returning this err
File "/home/olaneat/Desktop/files/project/django/job/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table
with self.connection.cursor() as cursor:
File "/home/olaneat/Desktop/files/project/django/job/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/home/olaneat/Desktop/files/project/django/job/lib/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor
return self._cursor()
File "/home/olaneat/Desktop/files/project/django/job/lib/python3.8/site-packages/django/db/backends/dummy/base.py", line 20, in complain
raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
below is my database setting
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'HOST': os.environ.get('PGHOST'),
'NAME': os.environ.get('PGDATABASE'),
'USERNAME': os.environ.get('PGUSER'),
'PASSWORD': os.environ.get('PGPASSWORD'),
'PORT':os.environ.get('PGPORT')
}
}
by the way, this is working on my localhost, i only get this err when i deploy to railway
can someone pls help out
Use this setting for postgresql:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
REF: https://docs.djangoproject.com/en/4.1/ref/settings/#databases
You need to add the relevant environment variable names to the railway app variable settings. To add variables click + New Variable and it will prompt you to enter the variable name and its value in it. Copy the Database Connection URL from the PostgreSQL service settings and paste in the app variables DATABASE_URL's value
If you think the method isn't working for you, you can also try the second approach.
You can add the database URL as a single string as well, this is also an alternative rather than specifying all the separate fields.
# pip install dj_database_url python-dotenv
import dj_database_url
import os
from dotenv import load_dotenv
load_dotenv(os.path.join(BASE_DIR, '.env'))
DATABASE_URL = os.getenv("DATABASE_URL")
DATABASES = {
"default": dj_database_url.config(default=DATABASE_URL, conn_max_age=1800),
}

OperationalError could not connect to server

I put a Django app on Heroku recently. The home page looks fine, but when I try to go to a page that involves making a query (e.g. p = Photo.objects.get(title=title)), I get this error:
could not connect to server: Connection refused
Is the server running on host "localhost" and accepting
TCP/IP connections on port 5432?
In accordance with this answer, I did $ heroku pg:promote HEROKU_POSTGRESQL_GREEN_URL
Then in my settings.py:
DATABASES = {'default': dj_database_url.config(default=os.environ['DATABASE_URL'])}
Still got the same error, so I tried looking at the results of this (as this answer suggests):
$ heroku run python manage.py shell
>>> from django.conf import settings
>>> print settings.DATABASES['default']
{'TIME_ZONE': 'UTC', 'TEST_MIRROR': None, 'NAME': 'snorthway', 'OPTIONS': {},
'HOST': 'localhost', 'TEST_NAME': None, 'PASSWORD': '******', 'ENGINE':
'django.db.backends.postgresql_psycopg2', 'PORT': '', 'USER': 'snorthway',
'TEST_COLLATION': None, 'TEST_CHARSET': None}
At which point I realized I don't know what I should even be looking for in that. I still don't understand what the error means, so I am unsure how to go about debugging it.
You have not configured your django database correctly in settings.py. It thinks your database is on localhost. Sounds like you have a heroku postgres database so your host should be something like:
df3-64-304-50-250.compute-1.amazonaws.com
Heroku exposes a special database URL through an environment variable called:
DATABASE_URL
There is a very cool python package here called dj_database_url: https://github.com/kennethreitz/dj-database-url it converts that environment variable to what django expects.
you can install it with:
$pip install dj-database-url
I use the following in my settings.py
import dj_database_url
DATABASES = {
'default': dj_database_url.config()
}

Heroku Database Settings Injection - How do I setup my dev django database?

I'm trying to get my local dev django app to work after following these instructions on adding env database settings.
https://devcenter.heroku.com/articles/django-injection
I followed the instructions but get the following error when my app tries to access the local database
Request Method: GET
Request URL: http://localhost:8000
Django Version: 1.4
Exception Type: ImproperlyConfigured
Exception Value:
You need to specify NAME in your Django settings file.
My database settings originally,
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db', # Or path to database file if using sqlite3.
'USER': 'foo', # Not used with sqlite3.
'PASSWORD': 'bar', # Not used with sqlite3.
'HOST': 'localhost',
'PORT': '5432',
}
}
the heroku article says to add the following to the settings file
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
how do I get dj_database_url.config to use my my dev settings when the DATABASE_URL is not available in dev?
You can just add your dev settings to the default values like this...
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://foo:bar#localhost:5432/db')}
Use this in your settings.py:
DATABASES = {'default': dj_database_url.config(default=os.environ['DATABASE_URL'])}
and in your .env file have this:
DATABASE_URL=postgres://localhost/yourdbname
when you launch with "foreman start" it will look at the .env file and create all those environment variables, just like running on Heroku itself. Type "heroku config" to confirm that you have a DATABASE_URL set, which you should if you added the postgres database addon.
Just set an environment variable on your operating system and check weither or not it's set. For instance, with a UNIX system:
# In ~/.bash_profile
export LOCAL_DEV=true
# In settings.py
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
if bool(os.environ.get('LOCAL_DEV', False)):
# Override DATABASES['default'] with your local database configuration
Also, if you need to set an environment variable on your heroku space:
heroku config:add MY_VAR='my_value'
I just tried this and here is my code:
import dj_database_url
local_db = 'postgres://django_login:123456#localhost/django_db'
DATABASES = {'default': dj_database_url.config(default=local_db)}
My database name is "django_db", user name is "django_login", password is "123456".
My code can run both in local machine and heroku.
import dj_database_url
DATABASES = {'default':
dj_database_url.config(default='postgres://yourusername:yourpassword#yourhosturl:5432/yourdbname')}
** Replace bold string with your database settings
if you are using local database then replace yourhosturl by localhost

Getting Django and PostgreSQL to work

I am trying to get Django and PostgreSQL to work.
So far I am getting the following error when I run syncdb.
....
django.core.exceptions.ImproperlyConfigured:
Error loading psycopg2 module: No module named psycopg2
The following is my settings.py.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'mydb', # Or path to database file if using sqlite3.
'USER': 'username', # Not used with sqlite3.
'PASSWORD': 'pwd123', # Not used with sqlite3.
'HOST': 'localhost', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '5432', # Set to empty string for default. Not used with sqlite3.
}
}
I think I have installed psycopg2 correctly, but I am not sure (through MacPorts).
Is there a way whether I can check whether psycopg2 is installed?
This link contains the install log of psycopg2
UPDATE
I got it working with the method below, but how do I check whether psycopg2 was actually installed before? and if so how to remove it completely?
If you are using MAC, make sure psycopg2 is installed and accesible to your main python interpreter.
This is how I'd install it on a mac:
$ sudo easy_install django
$ sudo easy_install psycopg2
Then test it:
$ python
>>> import django
>>> import psycopg2
You should not get any errors.
Also, if you are using an Eclipse/PYDEV, make sure you reconfigure your interpreter after installing django and psycopg2 libraries.