Configuring Postgres on Heroku with Django - python

I am trying to link a Heroku Postgres databse with my Django application. I have created the database and linked it to my Heroku application using this tutorial. I can't seem to get it working with Django, however.
I am able to access the database through the heroku pg:psql command. But when I try to run python manage.py migrate, Django gives...
settings.DATABASES is improperly configured
Please supply the ENGINE value.
In settings.py, I have...
import dj_database_url
DATABASES = {
'default': dj_database_url.config(default=os.getenv('DATABASE_URL'))
}
I have already promoted the correct heroku postgres database to my app's DATABASE_URL variable, which I can verify through heroku config.

You've misread the instructions. It should be:
DATABASES = {
'default': dj_database_url.config()
}
dj_database_url already parses the env var, you don't need to pass it explicitly.

Heroku config variables aren't available in Django settings.py when testing locally.
https://stackoverflow.com/a/21763381/3783608

Related

How to push local django database into heroku database

I have created django project with postgres database I have deployed my project into heroku server, and also migrate with
heroku run python manage.py migrate
I want to push my stored database to heroku database,
PGUSER=edpuser PASSWORD=1qazxsw2 heroku pg:push applications postgresql-curly-07168 --app application
but getting
> error shiv#shiv:~/Documents/projects/django_related_project/zoedp$
> heroku pg:push applications postgresql-curly-07168 --app application
> heroku-cli: Pushing applications ---> postgresql-curly-07168 ▸
> Remote database is not empty. Please create a new database or use
> heroku pg:reset
I also run command heroku pg:reset and again try again
this time I got error
shiv#shiv:~/Documents/projects/django_related_project/zoedp$ PGUSER=edpuser PASSWORD=1qazxsw2 heroku pg:push applications postgresql-curly-07168 --app application
heroku-cli: Pushing edpapplication ---> postgresql-curly-07168
pg_dump: [archiver (db)] connection to database "application" failed: FATAL: Peer authentication failed for user "edpuser"
pg_restore: [custom archiver] could not read from input file: end of file
▸ pg_dump errored with 1
here is my setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'applications',
'USER': 'edpuser',
'PASSWORD': '1qazxsw2',
'HOST': 'localhost',
'PORT': '5432',
'ATOMIC_REQUESTS': True
}
}
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=600)
DATABASES['default'].update(db_from_env)
and overwriting pg_hba.conf deny me.
what should I do?
I was having this issue, is more related to postgresql auth configuration rather than heroku pg:push command
I would recommend this approach:
Set a password for the postgres user:
Based on this amazing answer: https://stackoverflow.com/a/26735105/3172310
At this moment:
You have set a password for the postgres user and configure postgres to ask for id when using psql, which seems pg command does.
then run command as this:
PGUSER=postgres PASSWORD=pg_password heroku pg:push local_db_name DATABASE_URL --app application_name
Replacing:
pg_password: password for the postgres user
local_db_name: your local database
application_name: heroku app name
and should work fine!

Django py manage.py makemigrate

What can I make with it? I'm beginner in python and django. I download it and I i wrote py manage.py makemigrate and I've get error. Can u help me?
Your issue is with your DB configuration in the setting.py. If you are using the default SQLite then copy/paste this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
and your project will work just fine. After this, run
python manage.py makemigrations
python manage.py migrate #copy all migrations to the database
python manage.py createsuperuser #to have a admin user to login to adminpanel
python manage.py runserver #starting the server
Otherwise, take a look at the official documentation how to connect MySQL, PostgreSQL, Oracle databases and required configurations.
Your error is in here:
SQLite is not like MySQL or other databases. Actually, it is not a real database. You are using a port, username, password and etc. These are the cause of the error. SQLite is not running in the server or another place. It is just a single file contains data information. Update yours to mine above and it should start work again or change your database to MySQL or others.
You need to supply all environment variables that are listed in your settings file. Such as DB_NAME that presented in your screenshot. Search for os.environ[<VARIABLE_NAME>], every VARIABLE_NAME should be defined.
If you are a beginner it is better to stay with the documentation and do like https://docs.djangoproject.com/en/2.1/intro/tutorial01/
If you could share the DB part of the settings.py it would help.
Generally python manage.py startapp appname should create the necessary files for you.
After which a python manage.py makemigrations and python manage.py migrate should work properly. And this should not come.

Unable to config django for mysql

I'm trying to connect Django app to MySQL on windows 10. I have installed mysqlclient as an interface between my Django app and MySQL. Whenever I try to open dbshell I'm getting error something like this:-
django.core.exceptions.ImproperlyConfigured: Requested setting USE_I18N, but settings are not configured
You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Here is my setting.py file
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'blog',
'USER':'root',
'PASSWORD':'root',
'HOST':'127.0.0.1',
'PORT':'3306'
}
}
I have tried other solutions on StackOverflow but it didn't work for me.
I also tried deleting the dbsqlite3 file from the project directory.
Versions for different elements are as follows:-
Python (3.6)
Django (1.11.4)
mysqlclient (1.3.12)
pip (9.0.1)
MySQL(5.7)
DJANGO_SETTINGS_MODULE variable is not available. run
python manage.py shell
if you linux user and run
python3 manage.py shell
as you use python 3.6
Finally, I solved this problem. I was using django-admin dbshell command instead of python manage.py dbshell
In case of windows, set env variable for MySQL in user variables section, otherwise you will get CommandError saying that mysql is not installed.
For Ubuntu use python3 manage.py dbshell

Stylesheets and sqlite in Django/Openshift

I've got an Openshift/Django issue:
I'm having difficulty getting stylesheets and my SQLite database working in Openshift. When attempting to access a page that uses the database, I get the following Django debug message: "no such table: mytable". I get the same in rhc tail. Everything else appears to work.
My local file setup:
project
project
settings.py etc
app
normal django files and custom code here
templates
my templates are here
static
stylesheet is here
mydatabase.db
For openshift, I modify the directory as follows:
openshift appname
wsgi
project
project
settings.py etc
app
normal django files and custom code here
templates
my templates are here
static
stylesheet is here (NOT WORKING)
mydatabase.db (NOT WORKING)
Relevant bits of my setup.py file:
BASE_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), os.pardir)
STATIC_PATH = os.path.join(BASE_DIR, 'static')
DATABASE_PATH = os.path.join(BASE_DIR, 'mydatabase.db')
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': DATABASE_PATH,
}
...
STATIC_ROOT = ''
STATIC_URL = '/static/'
Any ideas? It works fine when running locally.
You'll want to checkout this blog post that goes over database configuration with django.
https://www.openshift.com/blogs/rapid-python-and-django-app-deployment-to-the-cloud-with-a-paas
You are likely getting the "no such table" error because the table does not actually exist (or the database itself does not exist). You do not mention running python manage.py migrate (or for Django before version 1.7 python manage.py syncdb). Without having run that command the table will not be automatically created.
In your case I think the database does not exist. Every time you do a git push to openshift, all files in your account will be overwritten except the special folder referenced in $OPENSHIFT_DATA_DIR. This is the only folder that is guaranteed to be saved and persisted across updates. You may have gone through the trouble of running migrate before and then on your next git push the database was destroyed because you had it set to be stored in your project directory. To ensure that your sqlite database is persisted across pushes you should update your settings file as follows:
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(os.environ['OPENSHIFT_DATA_DIR'], 'db.sqlite3'),
}
}
...
On Openshift you need to use action hooks to run the migrate command. This is done by creating a folder called .openshift/action_hooks inside of your project folder. Within the folder create an executable bash script called deploy.
project
.openshift
action_hooks
deploy
project
app
templates
static
The deploy file should contain the migrate and collectstatic commands. migrate will create the tables and collectstatic will copy all of the files in your static folder to the location specified in your settings under STATIC_ROOT. When running with DEBUG=false Django will not serve static files automatically. Openshift will use apache to serve the static files that are stored in wsgi/static so you should move the static folder up one level on Openshift and set STATIC_ROOT to point there. This should solve the stylesheet problem.
STATIC_ROOT = os.path.join(os.environ['OPENSHIFT_REPO_DIR'], 'wsgi/static')
project/.openshift/action_hooks/deploy:
#!/bin/bash
source $OPENSHIFT_HOMEDIR/python/virtenv/bin/activate
cd $OPENSHIFT_REPO_DIR
echo "Executing 'python manage.py migrate'"
python manage.py migrate
echo "Executing 'python manage.py collectstatic --noinput'"
python manage.py collectstatic --noinput

How to use dj-database-url while connecting with postgresql in heroku using python

I'm here because I'm really really new with heroku-python-django-postgresql group. I have googled for a usage for dj-database-url and I don't understand why i have to use it when developing a python application that needs to connect with postgresql. I have added postgresql (dev version) as add-on to my application, but I don't know how to tell to the app that I want it to use my db.
so, the short question is, How do I indicate to dj-database-url that I want to use my database?
Thanks for your time and answers, I'll appreciate your help because this is very very urgent!
dj-database-url is a utility to help you load your database into your dictionary from the DATABASE_URL environment variable. Heroku uses environment variables for your database and other addons. To begin using your database you'd simply use the below command to setup your DATABASES dictionary:
import dj_database_url
DATABASES['default'] = dj_database_url.config()
And maybe stash DATABASE_URL in your virtualenv activate script.
May be help for param in setting.py
DATABASES = {
"default": dj_database_url.config(
default="postgres://postgres:saleor#localhost:5432/saleor", conn_max_age=600
)
}
"database url" in default string have the value
postgresql://[user[:password]#][netloc][:port][/dbname][?param1=value1&...]
Use pip module dj-dtabase-url. Add an environmental var with the name 'DATABASE_URL' and the value from the heroku db settings.
Important, to avoid the error 'NameError: name 'DATABASES' is not defined.'
You still have to leave for example the default DATABSE settings.
The complete code:
import dj_database_url
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'mydatabase',
}
}
DATABASES['default'] = dj_database_url.config()

Categories

Resources