Django - settings.DATABASES is improperly configured. Please supply the ENGINE value - python

I've got a postgres DB which Django successfully connects to but when trying to create a new model I'm getting the error settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
I know settings.DATABASES is correctly configured as I've already created models which then Django used to create tables in the DB but for whatever reason it is now causing this error.
You can also see that I have already "supplied the ENGINE value".
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ['POSTGRES_DB'],
'USER': os.environ['POSTGRES_USER'],
'PASSWORD': os.environ['POSTGRES_PASSWORD'],
'HOST': 'db',
'POST': '5432',
}
}
All help is appreciated.

Replace your ENGINE setting by:
'ENGINE': 'django.db.backends.postgresql'
It is the correct setting in Django 3 and up. See https://docs.djangoproject.com/en/3.0/internals/deprecation/#deprecation-removed-in-3-0.

Turns out I'm an idiot.
I'm using docker and I forgot that I need to enter the container to do DB migrations 🤦‍♂️
The exact cause of this error is that the dockerfile creates an environment variable called "DJANGO_SETTINGS_MODULE" which Django looks for and the value is the name of an alternate file to be used for settings. As I wasn't in the docker container, this environment variable wasn't available so it was using the default settings.py which has had its DB settings removed.

Related

Can not connect to DB, after deleting old DB and all migrations

I replaced all URLFields in the models with Cloudinary fields, and decided to drop the DB and make new, because issues appeared. So every migration file, accept init files was deleted, I dropped the DB, even deleted all containers and volumes(I use docker for PostgreSQL). I have made new images and containers in docker. New database. But still can't connect to database nad migrate.
This is the error:
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more det ails.
This is my DB Settings:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'fishbook_db', 'USER': '*******', 'PASSWORD': '********', 'HOST': 'localhost', 'PORT': '5432', }, }
I have tried to fix the problem, by searching for this error in the web. I see a lot of people have the same problem, but I wasn't able to find solution so far.

`manage.py inspectdb` tells me that I don't have an ENGINE specified in my default database, but the docs say that default can be blank

I'm creating a django web app that is going to be a database management portal for multiple databases. Because the web app will touch multiple databases, it doesn't make sense to have a default. However, when I run manage.py inspectdb I get an error saying that the ENGINE value isn't set on my database. It most definitely is.
Here's my DATABASES setting in settings.py
DATABASES = {
'default': {
},
'my_db': {
'NAME': 'my_db',
'USER': 'user',
'PASSWORD': 'pass',
'HOST': '192.168.0.255',
'PORT': '',
'ENGINE': 'sql_server.pyodbc',
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
},
},
}
If I run manage.py inspectdb using this setup I get this error:
settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
This doesn't make sense to me, since it says in the documentation that 'default' can be a blank {}.
However, if I supply a dummy NAME and ENGINE variable to the default db, it seems to work fine for the default DB, but it ignores my_db.
If I set default to look at my_db's information I get a login error (so I know at least something is working right there, even if my creds are bad).
So, what am I getting wrong in my database setup here?
You need to specify a database for which you need to inspectdb
python manage.py inspectdb --database your_db_name
For more details see the docs

Django: authentication for migrations across multiple machines?

I have been working on a Django app locally and it's now time to deploy it to a staging machine.
My Postgres database on the staging machine has a different username and password to my local machine.
I have got the Django app running okay on the remote machine, except that the database has not been initialised.
I assume that I should do this with migrate, so I try running:
$ python manage.py migrate
But I see the following error:
django.db.utils.OperationalError: FATAL: no pg_hba.conf entry
for host "127.0.0.1", user "mylocalusername", database "mylocaldbname"
It's failing because it doesn't allow me to log in with mylocalusername.
I assume that mylocalusername etc must be coming from the migrations files? Certainly the local username isn't set anywhere else on the staging machine, either in my settings file, or on the actual database itself.
How can I set up this database on the staging server?
I guess one way would be to delete everything in migrations and create a new local migration. Is that what I need to do?
I thought migrations were supposed to checked into source code, though, so I'd rather not delete all of them. Also, I want to carry on working on locally and updating my staging and production machines, so I need to find a sustainable way of doing this.
"mylocalusername" comes from settings.py file.
It should looks like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'mylocaldbname', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'mylocalusername',
'PASSWORD': 'password',
'HOST': 'localhost', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
You can change it or create a valid user in your postgres database.

Mongoengine: ConnectionError: You have not defined a default connection

In my new Django project I set up a MongoDB database and use mongoengine module
but I can't properly access the dabase neither in shell nor in views.
"ConnectionError: You have not defined a default connection"
My settings.py includes the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DaTaBaSe',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '3306',
},
'tracking': {
'ENGINE': 'django.db.backends.dummy',
'NAME': 'analytics',
}
}
import mongoengine
SESSION_ENGINE = 'mongoengine.django.sessions'
mongoengine.connect(_MONGODB_NAME, 'localhost:27017')
AUTHENTICATION_BACKENDS = (
'mongoengine.django.auth.MongoEngineBackend',
)
In models.py:
import mongoengine
from mongoengine import *
from myproject.settings import _MONGODB_NAME
mongoengine.connect(_MONGODB_NAME, 'localhost:27017')
I have not tested this in-depth, but so far it worked for me:
mongoengine.connect('yourdb', alias='default')
In your settings.py file replace:
mongoengine.connect(_MONGODB_NAME, 'localhost:27017')
with the below code (notice the added 'host='):
mongoengine.connect(_MONGODB_NAME, host='localhost:27017')
To use django with MongoDB do not use the django package available on https://www.djangoproject.com and install other packages like mongoengine, if follow this process you will find lot of difficulties.
Rather you need to use the no#rel version of django that has been forked from djangoproject and added MongoDB support and I am sure it will make setup process and development process lot easier.
Follow this link to install and set up the django with MongoDB.
http://django-mongodb-engine.readthedocs.org/en/latest/topics/setup.html
One more thing you may find the error below, while setting up django.
"*Error on Django-nonrel and MongoDB: AutoField (default primary key) values must be strings representing an ObjectId on MongoDB (got u'1' instead). Please make sure your SITE_ID contains a valid ObjectId string.*"
Follow this link to fix.
https://gist.github.com/ielshareef/2986459
Please let me know if you need any more help on this.

Difficulty opening port 5432 for PostgreSQL on Ubuntu 12.04

I'm trying to get a postgres database talking to a django installation.
I've followed the steps details here: http://blog.iiilx.com/programming/how-to-install-postgres-on-ubuntu-for-django/
However, when I use syncdb to have django update my postgres database, I receive the following error:
connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: FATAL: database "/home/flowcore/django_db"
does not exist
django_db is the name of the database and it DOES exist but of course it doesn't exist at /home/flowcore/django_db (that is not where postgres stores data!)
My databases dict in settings.py looks like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.path.join(BASE_DIR, 'django_db'),
'USER': 'django_login',
'PASSWORD': 'mypasswordhere', #obviously i've replaced this with my actual password
'HOST': 'localhost',
}
}
Do I have to specific an absolute path to my postgres database file and, if so, where are these files stored?
Well, for some reason you have put the full path as the NAME parameter, so it's not surprising that Django is using that. There's no reason to do that, and that tutorial you link to doesn't suggest it. NAME is the database name itself, which as you say is just "django_db".

Categories