You should probably use environment variables (the prior link actually uses databases as an example. Using hardcoded values leaves you vulnerable to a bunch of different risks. The Django guide also presents connection files.
After you started to use that, then you need to figure out where you are running your Postgres database. localhost
means "my machine" (i.e the same machine as running the Django app.). If you are using some database-as-a-service, they'll expose all the environmental variables you need. If you are using something like Heroku, they will expose environment variables when running the service, that you'll probably use. If you are using a Kubernetes/Docker setup you yourself control, then you'll probably be in control of where the database is running and needs to use the path to that.
For heroku
I've used https://pypi.org/project/dj-database-url/ for a hobby project (which is no longer maintained, but does work, the last time I used it).
My config then looked like this:
DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": "mydatabase"}}
if "DATABASE_URL" in os.environ:
logger.info("Adding $DATABASE_URL to default DATABASE Django setting.")
DATABASES["default"] = dj_database_url.config(conn_max_age=600)
DATABASES["default"]["init_command"] = "SET sql_mode='STRICT_TRANS_TABLES'"
That gives you a working Sqlite3 version if no URL is added. You can use something else if you'd like. Heroku exposes an environment variable called DATABASE_URL
that links to the database you configured in Heroku, which you catch in if "DATABASE_URL" in os.environ:
, and then subsequently use. Did this provide a sufficient answer?