my app; react/django/postgres doesn't cxonnect to the database

Hi there:

I have an app react as frontend, Django/Python as the backend and postgres

i contenerized the frontend (called menufrontapp) and menubackapp (backend).

I’m using the PostgreSQL add on of fly.io.

The frontend and backend are deploying correctly from their docker containers, but the backend is not connecting with the database.

in the setting.py of menubackapp, the DATABASES option is:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": os.environ.get("DB_NAME", "menu_db"),
        "USER": os.environ.get("DB_USER", "postgres"),
        "PASSWORD": os.environ.get("DB_PASSWORD", <my password>),
        "HOST": "localhost",  # Use localhost as the host since the database is running within the same container
        "PORT": os.environ.get("DB_PORT", "5432"),
    }
}

I imported os:

import os

in the .env file I have:

DB_NAME=menu_db
DB_USER=postgres
DB_PASSWORD=<my password>
DB_PORT=5432
DATABASE_URL=postgres://postgres:new_passwor@menubackapp-postgres:5432/menu_db

when i created menubackapp-postgres, it said that postgres uses port 5432, but the proxy uses 5433

The logs for menubackapp say:

2023-07-29T18:51:43.135 app[17811612a54758] scl [info] nc: getaddrinfo for host "menubackapp-postgres" port 5432: Name or service not known

I tried using 5433 with the same result

the app menubackapp-postgres is deployed correctly (deployed)

why doesn’t it connect with the database

Thanks in advance

Rafael

To connect to your DB, the hostname should be menubackapp-postgres.flycast, not menubackapp-postgres

So, try this:

DATABASE_URL=postgres://postgres:new_passwor@menubackapp-postgres.flycast:5432/menu_db