PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 27, 2022

[FIXED] How to config the heroku.yml to use the Heroku PostgreSQL addon?

 September 27, 2022     continuous-deployment, django, docker, heroku, postgresql     No comments   

Issue

What I currently I do is to deploy my Django application to Heroku through Building Docker Images with heroku.yml. The app is built well, but it cannot connect to the database, because of the host is not configured correctly. My heroku.yml config is

setup:
  addons:
    - plan: heroku-postgresql
      as: DATABASE
build:
  docker:
    web: Dockerfile
  config:
    DJANGO_SETTINGS_MODULE: Django-BaaS.settings_docker
release:
  command:
    - python manage.py migrate
  image: web
run:
  web: gunicorn Django-BaaS.wsgi

My settings_docker is

from .settings_base import *

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'psql',
        'HOST': 'DATABASE',
        'PORT': '5432'
    }
}

My understanding is that heroku.yml is a file which is similar to docker-compose.yml so I tried to use DATABASE as the HOST name (and I tried to use localhost), but it looks like that both are not correct way to connect the database.

I seek some help to correct my config or setting files and how to use the PostgreSQL addon on heroku's docker CD pipeline. I am stuck here for a while. Thanks in advance,


Solution

I figured it out by myself. There are 2 mistakes I made.

  1. heroku.yml is like docker-compose.yml, but between addons in setup and build, there is no bridge. Keyword as is not equal to container_name.
  2. The container (in the case it is web), it can still directly access the psql addon, but the access url it is not 127.0.0.1 or localhost (which will try to visit the container itself), it is a dynamic value stored in environment variable.

How to make it work

The direct way is using dj_database_url, update the db setting to,

import dj_database_url
DATABASES['default'] = dj_database_url.parse(os.environ['DATABASE_URL'])

The better way is to use django_heroku, update the setting to,

import django_heroku
django_heroku.settings(locals())

References

  • Django-BaaS the project I am working on
  • dj-databse-url github
  • Deploying Python and Django Apps on Heroku
  • How to serve django static files on heroku with gunicorn


Answered By - tim
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing