Skip to content
Aditya Bhalsod
← DevOps
DevOpsBeginner

Adding a SQLAdmin Panel to a FastAPI and Postgres Docker Starter

A beginner how-to for fastapi-postgres-sqladmin-docker, covering the SQLAdmin mount, a model view, how DATABASE_URL reaches the app and what to change first.

  • #fastapi
  • #sqladmin
  • #docker-compose
  • #postgresql
Contents
  1. Get the database URL to the app
  2. Mount the admin panel
  3. Give a model an admin view
  4. What to change before real use
  5. Two small gotchas
  6. When this is enough

fastapi-postgres-sqladmin-docker is a small starter template. It has a FastAPI app with a CRUD API for one Item model, async SQLAlchemy on PostgreSQL, an SQLAdmin panel, and a Compose file that starts the lot.

This how-to covers the database URL, the admin panel and the model view, then lists what I would change before using it for anything real.

Get the database URL to the app

The app reads its connection string from one environment variable.

Python
DATABASE_URL = os.getenv("DATABASE_URL")

engine = create_async_engine(
    DATABASE_URL, echo=True, future=True
)

There is no fallback value, so the variable must be set. In Compose, the web service gets it from env_file: .env, so the whole file becomes the container's environment. The value comes from .env.example:

Bash
DATABASE_URL=postgresql+asyncpg://postgres:postgres@db:5432/fastapi_db

postgresql+asyncpg selects the async driver, and asyncpg is pinned in requirements.txt. db is the name of the Postgres service in docker-compose.yml, which Compose resolves as a hostname on the shared network. fastapi_db has to match POSTGRES_DB.

Copy the example and start everything:

Bash
cp .env.example .env
docker compose up --build

Mount the admin panel

The panel is two lines in app/main.py. Admin(app, engine) attaches it to the existing FastAPI app and hands it the same async engine the API uses. The comment in the file says it appears at /admin, and the README gives the same address.

Python
admin = Admin(app, engine)
admin.add_view(ItemAdmin)

There is no router to include and no mount call. Open http://localhost:8000/admin once the containers are up.

Give a model an admin view

A view is a class that subclasses ModelView and names its model with a keyword argument.

Python
class ItemAdmin(ModelView, model=ItemModel):
    column_list = [ItemModel.id, ItemModel.name, ItemModel.description]
    name = "Item"
    name_plural = "Items"
    icon = "fa-solid fa-table"

column_list picks the columns on the list page. name and name_plural are the sidebar labels, and icon is a FontAwesome class. For another model, write another class like this and pass it to admin.add_view.

The admin works from the SQLAlchemy model, not from the Pydantic schemas. The ItemCreate and Item schemas play no part in the admin. The routes and crud.py use them. The model's description column is nullable, but the Item response schema declares description: str. A row with a NULL description can exist, and the API would then fail to serialize it.

What to change before real use

  1. Protect /admin. Admin(app, engine) takes no authentication, and neither do the API routes. The README's FAQ says as much: "For production, secure /admin with authentication."
  2. Move the credentials. postgres and postgres appear in docker-compose.yml and .env.example. .env is git-ignored, but the example file with the same values is committed.
  3. Close the extra ports. Compose publishes Postgres on 5432 and Redis on 6379 to the host. The web container reaches the database by its service name, so the database port is only needed if you want to connect from your own machine.
  4. Remove or use Redis. There is a redis service, a REDIS_URL line and a depends_on entry. No Python file reads Redis, and requirements.txt has no Redis client.
  5. Turn off the development settings. echo=True prints every SQL statement. The Dockerfile starts uvicorn with --reload, and Compose bind-mounts ./app over /app.
  6. Use migrations. The tables come from create_all at startup, and no migration tool is in the requirements.

Two small gotchas

The imports are flat, such as from database import engine. The Dockerfile copies app to /app and runs main:app, so no package name is involved. The file app/__init_.py is empty and has a single trailing underscore, so it looks like a typo for __init__.py. It is harmless here because nothing imports app as a package.

Also, depends_on only orders container start-up. There is no health check and no restart policy on web, so on a first run with an empty volume the app can try to reach Postgres before it is ready, fail during startup and stay down.

When this is enough

This starter suits learning how the pieces connect, or a quick internal prototype that never leaves your machine. Once the data matters or the port is reachable by anyone else, the first job is authentication, and the second is moving off create_all.

Found this useful? Share it with your team.

Keep reading

More from DevOps and beyond