TECHNICAL REFERENCE STACK · 8 COMPONENTS

Secure containerized FastAPI and PostgreSQL deployment with reverse proxy and off-site backups

A self-hosted baseline for a Python FastAPI service backed by PostgreSQL. Docker packages the application and infrastructure services; Nginx terminates HTTPS and proxies only approved requests to FastAPI; PostgreSQL remains on a private network; encrypted logical database backups are copied to object storage. This is a design baseline and requires environment-specific operational validation.

Publication review2026-10-18 UTC
Latest official source retrieval2026-11-29 UTC
Validation levelReference architecture
Official documentation changed after publication. Recheck affected details before implementing this architecture.
↳
FROM THEORY TO IMPLEMENTATION

Executable examples and validation

No executable package has been validated for this architecture yet. The implementation sequence below is guidance, not a tested repository.

Validation badges are scoped to specific artifacts and recorded tests. They never certify a full stack unless that scope was actually tested.

01
TECHNICAL DESIGN

Architecture and implementation

Architecture

Public traffic reaches Nginx (70), which obtains and serves TLS certificates using Let's Encrypt (71) and reverse-proxies application requests to the FastAPI container (21). Nginx and FastAPI share a dedicated internal proxy network; FastAPI and PostgreSQL (31) share a separate internal database network. Nginx is not connected to the database network, and PostgreSQL is not exposed through the public proxy or host firewall. FastAPI accesses PostgreSQL through SQLAlchemy (89). Containers use Docker isolation (69); only Nginx HTTP and HTTPS listeners are published to the host. A scheduled, explicitly authorized backup process accesses PostgreSQL on the database network, creates consistent logical backups, encrypts them before transfer, and stores them in Amazon S3 (51). The Docker host may be provisioned on Hetzner Cloud (49).

Implementation sequence

  • Define the API contract, authentication and authorization model, data classification, retention rules, recovery objectives, and expected operational ownership before exposing endpoints.
  • Create the FastAPI service with explicit request and response schemas, centralized error handling, health and readiness endpoints, and configuration supplied through environment-specific secret handling rather than source control.
  • Model the PostgreSQL schema and controlled migrations using SQLAlchemy-compatible tooling. Use a least-privilege application database role; separate migration or administration credentials from runtime credentials.
  • Create container definitions and two internal Docker networks: a proxy network joined only by Nginx and FastAPI, and a database network joined only by FastAPI and PostgreSQL. Join an explicitly authorized backup or administration process to the database network only when required. Do not publish FastAPI or PostgreSQL ports to the host.
  • Publish only Nginx HTTP and HTTPS listeners to the host. Configure Nginx to proxy required routes to FastAPI over the proxy network, not the database network.
  • Configure Nginx to redirect HTTP to HTTPS, terminate TLS, enforce conservative request and timeout limits, and avoid forwarding untrusted client headers without an explicit trusted-proxy policy.
  • Configure Let's Encrypt certificate issuance and renewal, including a monitored renewal process and a safe Nginx configuration reload after certificate updates.
  • Provision the host on the selected infrastructure provider, apply host firewall rules, restrict administrative access, maintain operating-system and container image patching, and use persistent storage appropriate to PostgreSQL data.
  • Implement a scheduled backup workflow that performs PostgreSQL logical dumps through an authorized database-network context, verifies backup completion, encrypts archives before upload, transfers them to a restricted S3 location, and records backup job outcomes without logging secrets.
  • Periodically restore a backup into an isolated PostgreSQL instance, validate schema and representative data, document the recovery procedure, and exercise host-loss and credential-loss scenarios.
  • Add deployment checks for network membership and absence of FastAPI/PostgreSQL host-port publication, plus migrations, configuration validation, container health, TLS status, database connectivity, and backup freshness before treating a release as operational.

Security and operations

  • Do not expose PostgreSQL or FastAPI ports publicly or publish them to the host. Permit database-network access only from FastAPI and explicitly authorized, controlled administration or backup contexts; do not connect Nginx to that network.
  • Restrict the proxy network to Nginx and FastAPI. Configure Nginx to reach FastAPI only through that network and to expose only intended public routes.
  • Terminate TLS at Nginx, redirect plaintext HTTP, protect certificate private keys with restrictive filesystem permissions, and monitor certificate renewal failures.
  • Store database passwords, S3 credentials, encryption keys, and application signing keys outside source control. Use distinct, minimally privileged credentials for application runtime, migrations, backups, and storage access.
  • Use parameterized SQL through SQLAlchemy and validate all API inputs. Apply authentication, authorization, rate limiting or equivalent abuse controls, and restrictive CORS rules according to actual client requirements.
  • Ensure proxy forwarding rules define trusted upstreams so clients cannot spoof security-relevant forwarded headers.
  • Encrypt backups before off-site transfer, restrict deletion and read permissions for backup storage, retain backup metadata, and regularly test decrypt-and-restore procedures.
  • Treat logs, API documentation, error responses, and backup artifacts as potential sources of sensitive data; redact credentials and avoid logging tokens or complete personal records.
  • Patch the host, Docker runtime, application dependencies, Nginx, and PostgreSQL through a defined maintenance process. Pin and review container image sources and dependency updates.

Limitations

  • The catalogue does not provide a dedicated secrets-management product, a PostgreSQL-specific managed backup service, a firewall product, or a CI/CD product. These controls require host/provider capabilities or external tooling not represented in this stack.
  • Nginx, Docker, PostgreSQL, network membership, and backup scheduling require operational administration, capacity planning, patch management, monitoring, and incident response procedures.
  • Amazon S3 is suitable for off-site backup objects, but this design does not assert specific immutability, lifecycle, regional, encryption-key, or recovery features; these must be selected and verified in the storage configuration.
  • A single host is a potential availability and failure-domain risk. Encrypted off-site backups improve recoverability but do not create a highly available application or database deployment.
  • FastAPI and SQLAlchemy do not themselves establish authentication policy, authorization policy, migration tooling, background processing, or complete observability; these need application and operational design decisions.

Alternatives to consider

  • Use DigitalOcean (48) or GreatHosts (50) rather than Hetzner Cloud (49) for the infrastructure host, after evaluating operational and regional requirements.
  • Use Backblaze B2 (53), Cloudflare R2 (52), or Wasabi Hot Cloud Storage (95) rather than Amazon S3 (51) for encrypted off-site backup storage.
  • Use Supabase (38) for a managed or self-hostable PostgreSQL-centered backend direction, while assessing how its operational model and access controls fit the FastAPI deployment.
  • Add Sentry (56) for application error monitoring and OpenTelemetry (99) with Prometheus (100) and Grafana (57) for observability, recognizing that these introduce additional configuration and operational components.
  • Use Auth0 (42), Clerk (43), or self-hosted Keycloak (41) if the application needs a dedicated identity provider rather than application-managed authentication.

A reference architecture, not a tested deployment or guarantee of interoperability. Validate all external contracts, versions and prices before implementation.

IMPLEMENT THE REFERENCE

Build this architecture with an AI agent

Turn this source-backed reference into a project-specific plan and code, including tests and implementation boundaries. It has not been execution-tested by Saitat.

View agent implementation prompt
Plain text ↗

Reference guidance only; verify versions, services and unresolved prerequisites. Do not share private credentials with an AI service.

02
PROVENANCE

Sources and review dates

Official pages are recorded with UTC retrieval timestamps. Retrieval confirms access to a page, not verification of every technical claim, price, or license.

Amazon S3Documentation · page retrieved
At publication2026-10-17
Latest retrieved2026-11-28
Source ↗
DockerDocumentation · page retrieved
At publication2026-10-17
Latest retrieved2026-11-29
Source ↗
FastAPIDocumentation · page retrieved
At publication2026-10-16
Latest retrieved2026-11-27
Source ↗
Hetzner CloudDocumentation · page retrieved
At publication2026-10-18
Latest retrieved2026-11-29
Source ↗
Let's EncryptDocumentation · page retrieved
At publication2026-10-18
Latest retrieved2026-11-29
Source ↗
NginxDocumentation · page retrieved
At publication2026-10-18
Latest retrieved2026-11-29
Source ↗
PostgreSQLDocumentation · page retrieved
At publication2026-10-17
Latest retrieved2026-11-28
Source ↗
SQLAlchemyDocumentation · page retrieved
At publication2026-10-18
Latest retrieved2026-11-29
Source ↗