How to solve this error deploying to render.com: django.db.utils.OperationalError: could not translate host name "***" to address?

What I'm trying to do: Deploy my django app to render.com with a postgres database. I'm following the render guide: Getting Started with Django on Render.

Problem: I am getting a build failed log error saying the following: django.db.utils.OperationalError: could not translate host name "***" to address: Name or service not known (I have omitted the actual host name here).

What research I have done: I have searched the error extensively, however all of the highly rated solutions I have encountered so far are based on using Docker like this which I am not using.

settings.py (snippet):

import dj_database_url

DEBUG = 'RENDER' not in os.environ

if DEBUG:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': BASE_DIR / 'db.sqlite3',
        }
    }

if not DEBUG:

    DATABASES = {
        'default': dj_database_url.config(default='DATABASE_URL', conn_max_age=600)
    }

In my render.com environmental variables, DATABASE_URL is saved with the postgres URL given by render which includes the database name, hostname, username and password. It follows this format: postgres://USER:PASSWORD@INTERNAL_HOST:PORT/DATABASE

I make the following and it work fine for me:

if DJANGO_ENV == 'development':
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        }
    }
elif len(sys.argv) > 0 and sys.argv[1] != 'collectstatic':
    if os.environ.get('DATABASE_URL', None) is None:
        raise Exception('DATABASE_URL environment variable not defined')
    DATABASES = {
        'default': dj_database_url.config(conn_max_age=60, ssl_require=True)
    }

If you are using render.yaml for deploying then you have to specify the services region. The region should be same as your database region.

source: https://community.render.com/t/django-could-not-translate-host-name-to-address/6187/2

render.yaml

databases:
    - name: berry

services:
  - type: web
    name: berry-service
    plan: free
    env: python
    region: singapore
    buildCommand: "./build.sh"
    startCommand: "gunicorn core.wsgi:application"
    envVars:
      - key: DATABASE_URL
        fromDatabase:
          name: berry
          property: connectionString
      - key: SECRET_KEY
        generateValue: true
      - key: WEB_CONCURRENCY
        value: 4

Screenshots

enter image description here

Back to Top