Configure your Django Application to Use a Postgres Database

<aside> 🔥 You will need to do this for both the dev environment (we'll use a local Postgres database for that) and for the production environment, on Render.

</aside>

To set up Postgres in your dev environment, follow these instructions:

Using Postgres Locally

Create an account on Render

First you’ll need to set up a free account on Render.com. You do not need to enter credit card information to set up your account. We’ll use their free plan.

🚧 Free plan limits

Configure your app to be deployed on Render

<aside> ⚙ This assumes you’re using django-environ and have a .env file.

</aside>

Settings for Render

RENDER_EXTERNAL_HOSTNAME and RENDER will be set by Render in the production environment. We need to make sure that Django knows it can serve your application in production by adding the Render hostname to the ALLOWED_HOSTS setting.

# settings.py

# configure a default value that will be used in the development environment
env = environ.Env(
    # set casting, default value
    DEBUG=(bool, False),
    USE_S3=(bool, False),
		RENDER=(bool, False) # <--- add this 
)

...
# add this
if env("RENDER"):
    ALLOWED_HOSTS.append(env("RENDER_EXTERNAL_HOSTNAME"))

Add a management command to create a superuser in production

We can’t run commands on a command line on Render’s free tier, so we need to create a superuser programmatically as part of the build script. We’ll create a custom Django management command to do this.