Migrate DB on Railway: ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value - django

I want to migrate my db to Railway Postgres.
I put my DATABASE_URL as a env variable in prod.env file:
DATABASE_URL='postgresql://postgres:(my password here)F#containers-us-west-97.railway.app:6902/railway'
Here how I import it in my prod settings file:
DATABASE_URL = os.getenv("DATABASE_URL")
DATABASES = {
"default": dj_database_url.config(default=DATABASE_URL, conn_max_age=1800),
}
When I try to migrate the db:
./manage.py migrate --settings=app.settings.prod
I get an error:
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check the settings documentation for more details.
I used the same approach when I migrated my DB to Heroku, and it worked well.
I checked that correct value comes DATABASE_URL to prod settings file when I debug it.
I also added DATABASE_URL as a variable to my Railway project.
UPD. I tried to hardcode my DATABASE_URL in the settings file, and it worked well. But again, even when I print my DATABASES after this code:
DATABASES = {
"default": dj_database_url.config(default=DATABASE_URL, conn_max_age=1800),
}
I see that the values are correct.
How can I resolve this?

Related

Django runs fine locally but one field doesn't exist after deploying to Heroku

I'm still relatively new to django. In my website, I created a new field group in AnonymousStudent model. My Django website runs fine on my local machine, but after deploying to Heroku it says the group field doesn't exist.
I got couple of errors when trying to access to different parts in my website.
One says:
ProgrammingError at ...
column "group" of relation "student_anonymousstudent" does not exist LINE 1: ...", "last_active_time", "associate_experiment_id", "group") V...
Another one says:
ProgrammingError at ...
column student_anonymousstudent.group does not exist LINE 1: ...dent_anonymousstudent"."associate_experiment_id", "student_a...
And the other one says:
InvalidCursorName at ...
cursor "_django_curs_139758823008000_sync_1" does not exist
The traceback for the last one says:
/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/utils.py in _execute
86. return self.cursor.execute(sql, params)
▶ Local vars
The above exception (column student_anonymousstudent.group does not exist LINE 1: ...dent_anonymousstudent"."associate_experiment_id", "student_a... ^ ) was the direct cause of the following exception: /app/.heroku/python/lib/python3.7/site-packages/django/db/models/sql/compiler.py in execute_sql
1144. cursor.execute(sql, params)
It seems like a migration problem, but when I tried to run python manage.py makemigrations it says nothing changed.
settings.py
DATABASES = {
"default": {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3')
}
}
if 'HEROKU_ENV' in os.environ:
ALLOWED_HOSTS.append(".herokuapp.com")
DATABASES["default"] = dj_database_url.config(
ssl_require=True,
engine="django_postgrespool2"
)
Procfile
release: bash release-tasks.sh
web: daphne giks.asgi:application -b 0.0.0.0 -p $PORT -v2
release-tasks.sh
python manage.py migrate
python manage.py loaddata fixtures/publications.json
python manage.py loaddata fixtures/awards.json
I have couple of questions here:
Apparently I'm using two different databases for local and Heroku environments. But I've specified in my Procfile to auto run migrations when deploying. So is this really a migration problem?
Still about the database. Can you use django_postgrespool2 with sqlite3?
What exactly is the problem? And how to fix it?
Please let me know if more information is needed. Thank you in advance.

django 2.1 + PostgreSQL 11 + Python 3.7 - Cannot do makemigrations

I'm trying to create a geospatial database with geodjango and postgis following the recommendations of the book : Python Geospatial development, 3rd Edition of Erik Westra, in order to do it I'm trying to configure my django database and to connect it to my PostgreSQL db.
After having launched my PostgreSQL database, I've created my django project and django apps. From then I'd like to apply makemigrations command to my shared app with :
python manage.py makemigrations shared
But then I've go the following error :
File "C:\Users\[...]\Anaconda3\lib\site-packages\psycopg2\__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError
I've even tried to check migrations with showmigrations but it makes the same error message so I've absolutely no clue what's going on.
here's my settings.py file:
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'shapeeditor',
'USER': 'shapeeditor',
'PASSWORD': '(password)',
}
}
I've put (password) to hide the real one but I've checked it's the good one.
The NAME corresponds to the database name with a USER who has the same name
The shared app is written in INSTALLED_APPS so I've checked I didn't forget it.
I've looked at the many posts in StackOverflow about the error I got but it doesn't correspond to what I've facing here
After writing my own question I've found where it bugged...
my USER shapeeditor didn't have the privileges, so it couldn't work, just switched with postgres and I worked, I'll have to change privileges in order to make it work

After deploying to heroku

I have a webapp which is not yet complete but I recently deployed it to heroku. It uses:
Django
Rest-framework
Reactjs
Now, I have deployed deploy-heroku branch of my project to master of heroku.
The only difference between my project's master branch and deploy-heroku branch is that I have made additional changes in settings.py (adding prostgre sql settings and all) in the deploy-heroku branch.
I want to add more features to my webapp so should I work on master and later copy-paste those changes to deploy-heroku. This seems redundant !! Is there any other better way to do this?
You could just let Heroku automatic deploy on master and use a ".env" file with Django-environ (https://github.com/joke2k/django-environ) to change your settings.py. You should be able to create a local Django setting and a Heroku prod setting.
Example :
.env :
DEBUG=on
SECRET_KEY=your-secret-key
DATABASE_URL=psql://urser:un-githubbedpassword#127.0.0.1:8458/database
SQLITE_URL=sqlite:///my-local-sqlite.db
setting.py:
import environ
env = environ.Env(
# set casting, default value
DEBUG=(bool, False)
)
# reading .env file
environ.Env.read_env()
# False if not in os.environ
DEBUG = env('DEBUG')
# Raises django's ImproperlyConfigured exception if SECRET_KEY not in os.environ
SECRET_KEY = env('SECRET_KEY')
# Parse database connection url strings like psql://user:pass#127.0.0.1:8458/db
DATABASES = {
# read os.environ['DATABASE_URL'] and raises ImproperlyConfigured exception if not found
'default': env.db(),
# read os.environ['SQLITE_URL']
'extra': env.db('SQLITE_URL', default='sqlite:////tmp/my-tmp-sqlite.db')
}
Don't forget to add the .env file to your .gitignore and to update your Heroku environment variables in your app -> settings -> Reveal config vars
You can merge branches.
Here is a good explanation of how it works

Django sqlite database not gettitng created in local machine and in remote getting Secret key Error

I am having the below settings for database in settings/base.py. The behaviour is different in both my local Mac OS X and in ubuntu for the same code.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, '../database/db.sqlite3'),
}
}
In local OS X machine, i experience the below error:
File "/Users/user/.virtualenvs/p_dev/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 207, in get_new_connection
conn = Database.connect(**conn_params)
django.db.utils.OperationalError: unable to open database file
In ubuntu server, where i try to host it for test, it shows below error.
File "/home/user/.virtualenvs/p_live/lib/python3.4/site-packages/django/conf/__init__.py", line 120, in __init__
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
Full error code is in pastebin here: http://pastebin.com/ERw0cJPm
Kindly help here. I checked the database folder both in local and server, this is not getting created.
OSX Issue:
Make sure that your django server process has access to write on the configured location.
Ubuntu Issue:
The manage.py module can't load the settings file configured in it. You can try after explicitly adding the --settings=<project_name>.settings.py
(p_live) user#ip-xx-xx-xx-xxx:~/sites/site.com/source$ python manage.py runserver --settings=<project_name>.settings.py
<project_name> = Pacakage name (or Django project name) where settings.py exists.

Configure databases section in settings.py for Travis CI

I have a locally running Django 1.7 app with some tests, connecting
to MySQL
I configured Travis CI with this repo
Question:
I want to have to have a separate database for Travis , that is different from the one I use for development.
I tried adding separate settings in settings.py : default (for use with tests) and development (for use in dev boxes); and thought .travis.xml would use the 'default' when it ran migrate tasks.
But Travis CI errors out with the error : django.db.utils.OperationalError: (1045, "Access denied for user 'sajay'#'localhost' (using password: YES)")
I have no idea why it is trying to access my development db settings? I checked django1.7 docs, googled around but no luck.
Appreciate any help,
Thanks
My settings.py database section looks like the below :
DATABASES = {
'default': {
'ENGINE':'django.db.backends.mysql',
'NAME':'expenses_db',
'USER':'root',
'PASSWORD':'',
'HOST':'127.0.0.1',
'PORT':'3306',
},
# 'development': {
# 'ENGINE':'django.db.backends.mysql',
# 'NAME':'myapp_db',
# 'USER':'sajay',
# 'PASSWORD':'secret',
# 'HOST':'127.0.0.1',
# 'PORT':'3306',
# },
}
Note : When the 'development' section is commented, Travis CI build is green
My .travis.yml is pasted below:
language: python
services:
- mysql
python:
- "2.7"
env:
- DJANGO_VERSION=1.7 DB=mysql
install:
- pip install -r requirements.txt
- pip install mysql-python
before_script:
- mysql -e 'create database IF NOT EXISTS myapp_db;' -uroot
- mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost';" -uroot
- python manage.py migrate
script:
- python manage.py test
The problem you are getting is because you haven't got the right database name and settings for Travis CI. First you will need to separate out your settings between Travis and your project. To do that I use an environment variable called BUILD_ON_TRAVIS (alternatively you can use a different settings file if you prefer).
settings.py:
import os
#Use the following live settings to build on Travis CI
if os.getenv('BUILD_ON_TRAVIS', None):
SECRET_KEY = "SecretKeyForUseOnTravis"
DEBUG = False
TEMPLATE_DEBUG = True
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'travis_ci_db',
'USER': 'travis',
'PASSWORD': '',
'HOST': '127.0.0.1',
}
}
else:
#Non-travis DB configuration goes here
Then in your .travis.yml file in the before_script sections you will need to use the same database name as in the DATABASES settings. We then just have to set the environment variable in the .travis.yml file like so:
env:
global:
- BUILD_ON_TRAVIS=true
matrix:
- DJANGO_VERSION=1.7 DB=mysql
EDIT:
There is now an environment variable set by default when building on Travis.
Using this environment variable we can more simply solve the problem:
settings.py:
import os
#Use the following live settings to build on Travis CI
if os.getenv('TRAVIS', None):
#Travis DB configuration goes here
else:
#Non-Travis DB configuration goes here
Doing it this way is preferable as we no longer have to define the environment variable ourselves in the .travis.yml file.
Followed the approach suggested in http://www.slideshare.net/jacobian/the-best-and-worst-of-django to separate the settings for different environments
I checked out How to manage local vs production settings in Django? but did not get a clear answer. Thanks