Deploy Django with Postgres
This guide deploys a Django app to Naijacloud with a managed Postgres database. At the end you’ll have the app and the Django admin running over HTTPS at your own naijacloud.app address, with migrations applied every time the app starts and every push to your branch deploying automatically. The stack is Django 6, Gunicorn, WhiteNoise and Postgres 18, deployed from a GitHub repository.
djangopostgresA Django app and the Django admin on your own naijacloud.app address, reading and writing to managed Postgres. Open /admin to sign in.
Before you start
- A Django project in a GitHub repository. Starting fresh? Run
django-admin startproject mysite .and push it. - Python 3.12 or newer on your machine. Naijacloud builds Python apps on Python 3.12.
- A Naijacloud account. The free tier covers this guide: one web service and one database.
Add the production packages
Django’s development server isn’t built for real traffic. Add Gunicorn to serve the app, WhiteNoise to serve static files, and the two packages that let Django reach Postgres through a single DATABASE_URL.
Naijacloud finds requirements.txt, recognises Django in it and installs it with pip install -r requirements.txt. Commit the file with the next step.
Read settings from the environment
Open mysite/settings.py. Replace the hard-coded secret key, debug flag, hosts and database with values read from the environment, and give static files a folder WhiteNoise can serve.
From now on manage.py needs these variables too, so set them in your shell before running it locally: export DJANGO_SECRET_KEY=dev DJANGO_DEBUG=True DATABASE_URL=sqlite:///db.sqlite3.
Create the admin user on start
You’ll never need to connect to the database from your laptop. Instead, a short management command creates your admin user from two variables the first time the app starts, and does nothing after that.
Add "core" to INSTALLED_APPS in mysite/settings.py, then create the command:
Create the database
In the dashboard, open your project and choose New service → Database. Name it (for example mysite-db), pick Postgres, leave the connection details blank so they’re generated, choose the Free size and click Create database.
The next screen, mysite-db is ready., shows the connection details once. Click Reveal secrets and copy the Internal URL. Your app uses it to reach the database over the private network.
Create the web service
Back in the project, choose New service → Web service. Name it mysite; that name becomes your address, mysite.naijacloud.app. Under Source, choose GitHub and pick your repository and branch. Naijacloud detects Django and fills in the commands. Change them to these:
pip install -r requirements.txtpython manage.py collectstatic --noinput && python manage.py migrate && python manage.py ensure_admin && gunicorn mysite.wsgi --bind 0.0.0.0:$PORTEverything runs in the start command because your variables are set when the app starts. The command collects static files, applies migrations and creates the admin user before Gunicorn begins serving, so the database is ready by the time the app answers. Naijacloud sets PORT for you.
Set the variables
On the same screen, under Environment variables, click Paste .env and paste these five lines with your own values, then Add variables. Generate the secret key on your machine first:
.naijacloud.appDeploy and sign in
Leave Region and Instance type as they are. Free is enough for this guide. Free apps may sleep when nobody visits for a while and wake on the next request; paid apps never sleep. Click Create web service, then Open mysite. The Deploys tab streams the build log. When the deploy goes live, the start command’s output appears in Logs under Runtime:
Open https://mysite.naijacloud.app/admin and sign in with the username and password you set. From now on every push to your branch deploys, and the start command migrates the database first.