---
name: postgres-django
description: "Provisions a Postgres database (a Neon project of its own) for a Django app with `npx extraorbital add postgres` (no signup, no card) and wires it into Django with `dj-database-url` and `psycopg[binary]`. Use when a Django project needs Postgres, PostgreSQL, a SQL database, Neon, POSTGRES_URL, SQLAlchemy or psycopg, or when POSTGRES_URL_NON_POOLING is missing or unset in .env."
---

# Postgres in Django

<!-- Generated from ExtraOrbital's service registry by `pnpm skills:build` in platform/app. Edit the generator, not this file. -->

ExtraOrbital provisions a Postgres database (a Neon project of its own) in one command and writes the credentials into the project's env file. For anything this recipe does not cover, use the `extraorbital` skill or https://extraorbital.dev/llms.txt.

## 1. Provision

```bash
npx extraorbital add postgres
```

Run it from the project root. It is idempotent (a second run returns the same resource) and writes to `.env.local` if the project has one, otherwise `.env`, keeping that file out of git. Add `--json` to parse the result; credential values are redacted there, and you should never print them either.

| Variable | |
| --- | --- |
| `POSTGRES_URL` | Pooled: use it in the app |
| `POSTGRES_URL_NON_POOLING` | Direct: migrations, and anything that needs a session |
| `POSTGRES_HOST` |  |
| `POSTGRES_USER` |  |
| `POSTGRES_PASSWORD` |  |
| `POSTGRES_DATABASE` |  |

Free on the Prototype plan: 1 database, from the $5.00 account allowance.

## 2. Install the client

```bash
pip install python-dotenv dj-database-url "psycopg[binary]"
```

## 3. Connect

Django reads no env file by itself: `settings.py` loads it with `python-dotenv`, below `BASE_DIR`.

**`yourproject/settings.py`**

```python
import os

from dotenv import load_dotenv

# ExtraOrbital writes .env.local when the project has one, else .env.
# load_dotenv never overrides a variable already set, so the first file wins.
load_dotenv(BASE_DIR / ".env.local")
load_dotenv(BASE_DIR / ".env")

import dj_database_url

DATABASES = {
    # The direct URL: Django holds a session per connection and runs migrations,
    # neither of which a transaction pooler supports.
    "default": dj_database_url.parse(
        os.environ["POSTGRES_URL_NON_POOLING"], conn_max_age=60, conn_health_checks=True
    ),
}
```

## 4. Use it

**`yourapp/views.py`**

```python
from django.db import connection
from django.http import JsonResponse

# yourapp/urls.py: path("api/postgres/", views.check_postgres)


def check_postgres(request):
    with connection.cursor() as cursor:
        cursor.execute("select now()")
        (now,) = cursor.fetchone()
    return JsonResponse({"now": str(now)})
```

## 5. Verify

```bash
npx extraorbital list        # the resource is listed as active
python manage.py runserver
curl http://localhost:8000/api/postgres/
```

A JSON answer means the credentials, the client and the route all work. Delete the route afterwards if the app does not need it.

## Pitfalls

- Point migration tools at `POSTGRES_URL_NON_POOLING` (Prisma's direct URL, `drizzle-kit`, Alembic, `manage.py migrate`); through the pooler they fail in confusing ways.
- Not `DATABASE_URL`: ExtraOrbital never claims that name. If the code already reads it, change the code or copy the value of `POSTGRES_URL` into it yourself.
- Scale-to-zero: the first query after five idle minutes takes a moment longer. Storage still bills while idle, from the account allowance on Prototype.
- `yourapp` and `yourproject` stand for the project's own app and settings package.
- Read credentials once in `settings.py` and import `django.conf.settings` elsewhere; `os.environ[...]` fails at startup when one is missing, which is the point.
- Exit code 3 means the account is past its free allowance: show the human the link the command printed and wait. Do not retry or work around it.

Docs: https://extraorbital.dev/docs/resources/neon.md
