PostgreSQL & Heroku - cannot connect to the database - django

I'm trying to add a column to a table via my Django app with South but keep getting the following error upon running the python manage.py migrate <app name> command:
conn = _connect(dsn, connection_factory=connection_factory, async=async)
psycopg2.OperationalError: could not translate host name "ec2-107-21-99-105.comp
ute-1.amazonaws.com" to address: Temporary failure in name resolution
Does anybody have an idea why this is happening? I'm a newbie to both South AND the PostgreSQL database management system (which Heroku uses), so I am more than a bit confused.

Make sure you have defined your default database in settings.py like this:
DATABASES = {
'default': dj_database_url.config(default=os.environ.get('DATABASE_URL'))
}

Related

Access Django API via Heroku returns "detail": "Unsupported media type \"application/x-www-form-urlencoded\" in request."

I deployed my Django project on Heroku with gunicorn. It's basically only the api (no templates).
When I deploy heroku and access <heroku url>/api/login for example in the browser
and post login data already in json format, it always returns
"detail": "Unsupported media type \"application/x-www-form-urlencoded\" in request."
But when I do the same on localhost, the user gets authenticated and I receive a response with user data...
post data example for login:
{ "email": "ana#test.com", "password": "ana1234567890" }
The parser_classes = [JSONParser] is added on every view where I don't have images or files (there I am using FileUploadParser).
I deployed on Heroku with gunicorn, because on localhost I permanently received "Unauthorized" from backend ( Permission Class is "isAuthenticated" for most of my views). After some research I figured out that probably the authorization header is not sent (here the APACHE WSGIPassAuthorization On would be a solution) but I don't have an apache server running and I don't want no webserver running on my machine.
I thought if I would deploy it on heroku with gunicorn, I could continue with the development without the "Unautorized" header but instead I run in other errors, like "detail": "Unsupported media type \"application/x-www-form-urlencoded\" in request." .. or the database connection is refused.
In my frontend I am sending the following header
// Headers
const token_config = {
headers: {
'content-type': 'application/json',
accept: 'application/json',
'Authorization': 'Bearer ${token}',
//withCredentials : true,
},
};
And the frontend is of course also receiving the "Wrong mediatype Error" .
I would be so greatfull if anybody could provide an detailed "howTo" for Django DRF + postgres on Heroku... I've wen't through lots of howtos, but none of them provides a fitting solution for this..
My main questions now are...
What does settings.py need so that the backend works on heroku just as fine as on localhost?
Do I really need to start the heroku postgres database manually with
heroku pg:psql postgresql-shaped-60432 --app every time after deployment? - Because If I don't, the herocu log shows:
2020-09-09T11:11:15.028985+00:00 app[web.1]: connection = Database.connect(**conn_params)
2020-09-09T11:11:15.028985+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py", line 126, in connect
2020-09-09T11:11:15.028986+00:00 app[web.1]: conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
2020-09-09T11:11:15.028986+00:00 app[web.1]: django.db.utils.OperationalError: could not connect to server: Connection refused
2020-09-09T11:11:15.028987+00:00 app[web.1]: Is the server running on host "localhost" (127.0.0.1) and accepting
2020-09-09T11:11:15.028987+00:00 app[web.1]: TCP/IP connections on port 5432?
With this error + accessing the /api/login for example, I get an HTTP status of 503 or 500...
My requirements.txt:
Django==3.1.1
django-allauth==0.42.0
django-cors-headers==3.5.0
django-environ==0.4.5
django-rest-auth==0.9.5
django-sslserver==0.20
djangorestframework==3.11.1
djangorestframework-simplejwt==4.1.3
gunicorn==20.0.4
image==1.5.27
importlib-metadata==1.7.0
oauthlib==3.1.0
Pillow==7.2.0
psycopg2-binary==2.8.2
PyJWT==1.7.1
Thanks in advance and best regards!
Ok I fixed it!
I basically had to add all the settings.py stuff:
import django_heroku
import dj_database_url
ALLOWED_HOSTS = ['*']
DATABASES = {'default': dj_database_url.config(default='URI from Heroku-color- postgres database')}
# ALL THE STATIC PATHS
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'
#location where django collect all static files
STATIC_ROOT = os.path.join(BASE_DIR,'static')# location where you will store your static files
STATICFILES_DIRS = [os.path.join(BASE_DIR,'walkAdog/static')]
STATIC_URL = '/static/'
# for deployement on heroku
django_heroku.settings(locals())
AT THIS POINT the origin error “detail”: “Unsupported media type \”application/x-www-form-urlencoded\“ in request.” did not appear anymore
.. But the next error occured: No module named 'django_heroku'...
So I figured out, that django-heroku was not installed correctly, since the pipenv install psycopg crashed - worked for me as I did installed pipenv install psycopg2==2.7.7 and then django whitenoise dj-database-url psycopg2
instead, the database was "empty" - quite predictable..
So I did makemigrations on localhost
then commit the project/migrations folder (and the rest)
and ran heroku run python manage.py migrate
-> everything working fine now!!
Basically, I just had to follow the steps from https://medium.com/#hdsingh13/deploying-django-app-on-heroku-with-postgres-as-backend-b2f3194e8a43 .. I just kinda didn't understand the neccessarity of all the stuff..

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.db.utils.OperationalError: FATAL: database does not exist (postgres / deploy to digitalocean)

I am trying to deploy a project with digital ocean. I followed the instructions found at https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04
Some of the important ones:
I ran:
postgres=# CREATE DATABASE jobzumoDB;
CREATE DATABASE
then:
postgres=# CREATE USER admin WITH PASSWORD '123';
CREATE ROLE
postgres=# GRANT ALL PRIVILEGES ON DATABASE jobzumoDB TO admin;
GRANT
set the following in settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'jobzumoDB',
'USER':'admin',
'PASSWORD':'123',
'HOST':'localhost',
'PORT':'',
}
then tried to run:
~/jobzumo/manage.py makemigrations
and got:
File "/home/justin/jobzumo/env/lib/python3.6/site-packages/psycopg2/__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: FATAL: database "jobzumoDB" does not exist
Two things:
ALLOWED_HOSTS = ['jobzumo.com', '142.93.184.125']
I have not yet connect jobzumo.com to digital ocean, but the IP address was copied from my droplet.
Also, I ran: pip install django gunicorn psycopg2 (from digitial ocean)
but a tutorial on youtube said it was very important to now
install psycopg2-binary instead, however, I did not do this as the video was veering far from digital ocean's tutorial.
Thanks for any help, after starting to understand django I didn't think deploying would be this much of a headscratcher.
Formalizing what we worked out in the comments as an answer, when you give postgres an unquoted string as an identifier, it forces it to lower-case. see this similar answer from pgsql-general mailing list. So the actual name of the database created by the command CREATE DATABASE jobzumoDB; is jobzumodb. to create a database named jobzumoDB it's necessary to use quotes, as in CREATE DATABASE "jobzumoDB";
Use:
NAME': 'jobzumodb',
Instead of:
NAME': 'jobzumoDB',

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

South error when working with multiple databases: django.db.utils.ConnectionDoesNotExist: The connection foo doesn't exist

I have 2 Django projects with following db settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db1', # 'db2' for second db
...
}
}
When trying to sync second db with command
python manage.py syncdb --database=db2
I receive error
django.db.utils.ConnectionDoesNotExist: The connection db2 doesn't exist
When I use some other commands, South uses migrations from first project and fills db2 with wrong tables. How to correctly sync/migrate several projects served by single Django + South instance?
The database syncing method does not take the NAME key in the --database option. As specified earlier, default for your db1 only works.
So you need to setup an additional database dictionary for your db2.