3

I deployed on Heroku my project in Docker with Angular 4 frontend, Django backend and Postgresql database. At this moment my files look as shown below. When I open app I get error:

2017-07-11T19:51:14.485577+00:00 app[web.1]:     self.connect()
2017-07-11T19:51:14.485577+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
2017-07-11T19:51:14.485578+00:00 app[web.1]:     six.reraise(dj_exc_type, dj_exc_value, traceback)
2017-07-11T19:51:14.485578+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
2017-07-11T19:51:14.485578+00:00 app[web.1]:     raise value.with_traceback(tb)
2017-07-11T19:51:14.485579+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
2017-07-11T19:51:14.485579+00:00 app[web.1]:     self.connect()
2017-07-11T19:51:14.485579+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 189, in connect
2017-07-11T19:51:14.485580+00:00 app[web.1]:     self.connection = self.get_new_connection(conn_params)
2017-07-11T19:51:14.485580+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection
2017-07-11T19:51:14.485580+00:00 app[web.1]:     connection = Database.connect(**conn_params)
2017-07-11T19:51:14.485581+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
2017-07-11T19:51:14.485581+00:00 app[web.1]:     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
2017-07-11T19:51:14.485582+00:00 app[web.1]: django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known

Locally everything seems to be working properly. I use docker exec -ti name /bin/bash then python {path}\manage.py migrate and database is added.

Maybe there is a problem with my database migration on Heroku?

Procfile:

web: sh -c 'cd PROJECT/backend/project && gunicorn project.wsgi --log-file -'

Project tree:

DockerProject
      ├── Dockerfile
      ├── Procfile
      ├── init.sql
      ├── requirements.txt
      ├── docker-compose.yml
      └── PROJECT
            ├── frontend
                   └── all files
            └── backend
                  └── project
                        ├── prices
                        ├── manage.py
                        └── project
                              └── all backend files

Frontend's Dockerfile:

# Create image based on the official Node 6 image from dockerhub
FROM node:6

# Create a directory where our app will be placed
RUN mkdir -p /usr/src/app

# Change directory so that our commands run inside this new directory
WORKDIR /usr/src/app

# Copy dependency definitions
COPY package.json /usr/src/app

# Install dependecies
RUN npm install

# Get all the code needed to run the app
COPY . /usr/src/app

# Expose the port the app runs in
EXPOSE 4200

# Serve the app
CMD ["npm", "start"]

Main directory's Dockerfile:

FROM python:3.6.1
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip3 install -r requirements.txt
ADD . /code/

docker-compose.yml:

version: '3'

services:
  db:
    image: postgres
    environment:
      POSTGRES_USER: aso
      POSTGRES_PASSWORD: somepass
  django:
    build: .
    command: python3 MainDirectory/backend/myProject/manage.py runserver 0.0.0.0:8001
    volumes:
      - .:/code
    ports:
      - "8001:8001"
    depends_on:
      - db
  angular:
    build: MainDirectory/frontend
    ports:
      - "4200:4200"
    depends_on:
      - django

init.sql:

CREATE USER myUser;
CREATE DATABASE myProject;
GRANT ALL PRIVILEGES ON DATABASE myProject TO myUser;

UPDATE

My project including postgresql database was working properly with this setting. However as I have mentioned there was a problem with Heroku.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'aso',
        'USER': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
    }
}

Based on this topic I changed my database settings to

DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}

Unfortunately, I get:

ImproperlyConfigured at /
settings.DATABASES is improperly configured. Please supply the NAME value.

Any suggestions?

4
  • you made a superuser on that database? You also know thats a local database. If you want the one Your Heroku app is using you have to change the ENGINE parameter. Commented Jul 13, 2017 at 1:08
  • Locally I get the same error. I do not understand how django has to connect to the database as there are no parameters defined as in the previous version of settings? Commented Jul 13, 2017 at 1:16
  • did you made a superuser for that Postgres? Please answer the Question. If you want to run to run PSQL local you have to downloaded it and run a server. You did all that? Commented Jul 13, 2017 at 1:22
  • @hansTheFranz I have updated my post. With old settings database was working properly locally. Commented Jul 13, 2017 at 1:30

2 Answers 2

3

I want to help you and I give you here what you could change to make it wok but I cannot promise anything. I know you are now trying quite a long time to make that app run.

Make it run local

Change the DATABASES to the following:

DATABASES = {
 "default": 
 {
  "ENGINE": "django.db.backends.postgresql_psycopg2", #one of those should work
  'ENGINE': 'django.db.backends.postgresql',   #one of those should work
  "NAME": 'myProject',
  "HOST": "localhost", 
  "PORT": "5432",
 }
 }

Of course only choose one Engine and comment the other one out.

You can also try to access your DB over a desktop application like PSequel there you can login to your db with the given parameters.

Directly take the Heroku DB

Heroku provides a DB when you initialize a Project. You should have a Postgres Add-on. go to that link "https://dashboard.heroku.com/apps/YourAppName/settings" (the settings page ob your app on Heroku.com) and there you got the point "Config Variables" click on the button next to it. There should pop out a field with DATABASE_URL copy the link next to it with the format postgres://fasdfsdafsadfdsafsdafsda:123123132132123asdfdsafdssafdsfdaasdfsadf2-321-21-312-321.casdte-1.amazonaws.com:5432/adsdasdasads123 NOTE! Never give that link to anybody!

now you can use in your settings.py

  import dj_database_url 
  DATABASES = {'default': dj_database_url.parse('postgres://the-just-copied-link-comes-here

With that in your settings.py you have the Heroku DB in the Project. Its very slow for development but you need to send it do it this way when you push the code to Heroku. Don't forget to install dj_database_url if don't have it. Package

I hope that brings you a step closer to your goal.

in settings.py

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
MAILER_LIST = ['[email protected]']
EMAIL_HOST = ' smtp.gmail.com'
EMAIL_HOST_USER = 'Name'
EMAIL_HOST_PASSWORD = 'lalalala'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'root@localhost'
ADMINS = [("Name", '[email protected]')]
LOGGING = {
     'version': 1,
     'disable_existing_loggers': False,
     'handlers': {
         'mail_admins': {
             'level': 'ERROR',
             'class': 'django.utils.log.AdminEmailHandler' }},
     'loggers': {'django.request': {
             'handlers': ['mail_admins'],
             'level': 'ERROR',
             'propagate': True, },}}

You also don't have to do that now but with this you can see the errors you are getting on heroku in the logging section. There you have the traceback and can figure out why your fronted isn't working ( right now im guessing its a 404 with some static files). Tell me when you wrote your next question ;) Get to logs from your heroku app https://dashboard.heroku.com/apps/YourAppName/logs

Sign up to request clarification or add additional context in comments.

16 Comments

When I click on the button Reveal Config Vars next to Config Variables I get There are no config vars for this app yet. There is no DATABASE_URL.
heroku addons:create heroku-postgresql:hobby-dev put this in the terminal. Than you have it. You cannot have a Site on the internet which is viewable for everybody without a server.
I think I just took a step forward as you wrote. But there is another problem :) LINE 1: ...user"."is_active", "auth_user"."date_joined" FROM "auth_user... Lack of migration?
when you want to add the real Postgres Heroku database you need to make migrations and migrate again and make a user. You need to do all that anyway but im a bit afraid im pulling up a new problem before the old one isn't solved. What happened when you tried my local settings? Not able to connect?
I am not sure what is going on. When I open app my frontend is not working. But for instance when I do https://myapp.herokuapp.com/admin I see Django administration panel but logs are wrong and I getPlease enter the correct username and password for a staff account. Note that both fields may be case-sensitive. Locally everything seems to be working properly. However I had database with data before, at this moment Django created a new one, but it looks ok.
|
0

I ran into a similar problem and was able to deploy a django app to heroku using the following process:

  1. I created a new heroku app in my project's directory and pushed the code.
  2. At this point, heroku detected that my app was a django app which used postgres, so it automatically added the postgres addon. If your app doesn't do this, you can manually add the heroku postgresql addon.
  3. Now, the key step is to get the configuration information from heroku's instance of postgres and add it to the settings.py file in the django app. To do this, go to https://dashboard.heroku.com/apps//resources . "Heroku Postgres :: Database" should be listed under the "add-ons" section. Click on that.
  4. This will take you to the page for the "Heroku Postgresql" addon. Click on the "Settings" tab. Click the "View Credentials...". This will open a screen with config. information about the postgres instance.
  5. Copy these values (things like host, user, port, password, etc.) into the settings.py file in your django app and push it to heroku. Specifically to your question, the "Database" entry in this section provides the name of the database.
  6. Cross your fingers and hope for the best!

As described here, this can all be done programatically via the CLI.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.