Olney1
Olney1

Reputation: 772

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=os.environ.get('DATABASE_URL'),
    )
  }

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

Upvotes: 0

Views: 1777

Answers (3)

Femolak
Femolak

Reputation: 51

The error is because your servers are hosted in different regions, so your host name must contain the hostname+server URL e.g. dpg-ch1ecbbut4m01rpg4ud0-a.frankfurt-postgres.render.com

Extract this from the External Database URL.

I hope this helps someone.

Upvotes: 0

Mominur Rahman
Mominur Rahman

Reputation: 168

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

Upvotes: 2

Mahmoud Nasser
Mahmoud Nasser

Reputation: 850

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)
    }

Upvotes: 0

Related Questions