Skip to content

Madeeha-Anjum/multi-tenancy-system

Repository files navigation

Multi-Tenant System with FastAPI and Postgres 🚀

FastAPI PostgreSQL SQLAlchemy Alembic Docker Docker Compose Google Cloud Platform Pydantic Uvicorn Pytest Git VSCode Swagger UI Markdown

A Scalable Web Application with Efficient Database Management

A website, that serves multiple users or groups, also known as tenants.Each tenant company1.example.com has its own database schema. Fork this repository to get started with a multi-tenant system using FastAPI, PostgreSQL, Alembic, Docker, Ruff, Pdm, Typer, and more.

Installation

Pre-requisites 📋

  1. Clone the repository

  2. Install PDM (Python Development Master) package manager:

    pip install pdm --user
  3. Create and activate the virtual environment

    pdm venv create --with venv
      pdm use
    eval $(pdm venv activate )

Getting Started 🔧

Install python dependencies

  • May need to upgrade: rustup default nightly && rustup update

       pdm install --no-self

Setup .env file

Create a .env file in the root directory and add the following environment variables:

ENVIRONMENT=development

DB_NAME=db_name
DB_USER=user
DB_PASS=root
DB_PORT=5432
DB_HOST=localhost

PGADMIN_DEFAULT_EMAIL=user@user.com
PGADMIN_DEFAULT_PASSWORD=password

Setup Local Postgres Database

  • Alternatively you can setup a GCP database following the instructions in [.guide/gcp_database]
  1. Start postgres database in docker

    docker-compose -f .local_database/postgres_compose.yml --env-file .env up
  2. Login to PG Admin

    • open localhost
    • login with the credentials you provided in the .env file
      • PGADMIN_DEFAULT_EMAIL
      • PGADMIN_DEFAULT_PASSWORD
  3. Setup PGAdmin Access to database

    Add a new server Register->Server with the following details:

    General:
      # or any name you prefer
      Name: myserver
    Connection:
      # as configured in .local_database/postgres_compose.yml
      Port: 5432 (from docker container)
      Hostname/address: db
      Username: postgres
      Password: postgres

Run Alembic Migrations to create Database schemas and tables

  1. Run alembic migrations:

    alembic upgrade head

Create Tenants in the Database

  1. Run management command

    • python manage.py --help for more information
    python manage.py tenant create_tenant company1 company1 company1.localhost

Running the Application 🏃

  1. Run the application

    pdm run start
    # equivalent to: uvicorn multi_tenancy_system.main:app --reload --app-dir ./src
  2. Access the interactive API documentation:

Tools and Technologies 🛠️

fastapi postgresql sqlalchemy docker cloud pytest github visualstudio swagger markdownguide

  • PDM
    • Modern Python package manager, Specify dependencies in a pyproject.toml file.
    • add package in pyproject.toml file pdm add package_name
    • add package to dev dependencies pdm add package_name --dev
  • Typer - Typer is a library for building CLI applications that users will love using and developers will love creating.
  • Ruff - A simple and fast web framework for Python
  • Alembic
  • Pydantic - Data validation and settings management using Python type hints.
  • Uvicorn - ASGI server implementation, using uvloop and httptools.

License 📄

This project is licensed under the MIT License - see the LICENSE file for details.

About

FastAPI Backend with Postgres

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors