0

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

3 Answers 3

2

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

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

3 Comments

This is really helpful, my database is in Oregon and my Web Service is in Frankfurt. I'm in Settings > General in the Render.com dashboard but I can't see an option to change the region. Let me create a new Web Service and I'll confirm a bit later if this works.
"The region of a service cannot be changed once it's created.", You can't change the region. You have to create a new one.
Thank you, I can confirm that having the Web Service and Database in different regions was the cause of this error for me.
0

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

1 Comment

Thanks for the quick response, I tried this with and without your revised conditionals adapted to my code but I get the same error. Interesting that you don't have any config other than connection and ssl parameters and this still works?
0

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.