This project is a FastAPI application that uses PostgreSQL as the database and Alembic for schema migrations. It also includes a Docker configuration that lets you easily run your application in a container—using separate environment files for development and production.
Ensure you have Python 3.9+ installed, then install dependencies:
pip install -r requirements.txtpython -m venv .venv
source .venv/bin/activate # On macOS/Linux
.venv\Scripts\activate # On Windowsbrew services start postgresql@14 # For macOS (Homebrew)psql -U postgresCREATE DATABASE nexux_db;
CREATE SCHEMA master_data AUTHORIZATION confidential_db_user;CREATE USER confidential_db_user WITH PASSWORD 'password';
GRANT ALL PRIVILEGES ON DATABASE nexux_db TO confidential_db_user;
GRANT USAGE, CREATE ON SCHEMA master_data TO confidential_db_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA master_data
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO confidential_db_user;psql -U confidential_db_user -d nexux_db -h localhost -c "\dn" # List schemas
psql -U confidential_db_user -d nexux_db -h localhost -c "\dt master_data.*" # List tablesuvicorn true_app.main:app --host 0.0.0.0 --port 8000 --reload- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
Alembic is used to manage database schema changes.
alembic init alembicEdit alembic/env.py and ensure the following imports exist:
from true_app.database import SQLALCHEMY_DATABASE_URL, Base
from true_app.models import Role, Category, Component, Country, Document, Tenant
from true_app.models.associations import category_document, component_categorySet the correct metadata:
target_metadata = Base.metadataalembic revision --autogenerate -m "Added master_data schema"alembic upgrade headalembic historyalembic downgrade -1Check the current applied migration:
psql -U confidential_db_user -d nexux_db -h localhost -c "SELECT * FROM alembic_version;"Reset Alembic version (if a migration file was manually deleted):
DELETE FROM alembic_version;
INSERT INTO alembic_version (version_num) VALUES ('latest_revision_hash');Find latest_revision_hash using:
alembic historyalembic checkpsql -U confidential_db_user -d nexux_db -h localhost -c "\dt master_data.*"alembic downgrade base
alembic upgrade headbrew services list # For macOS
systemctl status postgresql # For LinuxThis project includes a Docker configuration that allows you to run your FastAPI application in a container. It also supports switching between development and production environment configurations.
Your Dockerfile is configured to:
- Install dependencies (including system packages)
- Copy your application code and environment files
- Run an entrypoint script that applies Alembic migrations before starting the server
Example Dockerfile snippet:
FROM python:3.9-slim
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
RUN apt-get update \
&& apt-get install -y gcc libpq-dev netcat-openbsd \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY requirements.txt .
RUN pip install --upgrade pip && pip install -r requirements.txt
COPY . .
COPY ./entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh
EXPOSE 8000
ENTRYPOINT ["/app/entrypoint.sh"]
CMD ["uvicorn", "true_app.main:app", "--host", "0.0.0.0", "--port", "8000"]Your entrypoint script applies Alembic migrations before launching the server:
#!/bin/sh
echo "Running Alembic migrations..."
alembic upgrade head
exec "$@"Your docker-compose.yml for development might look like this:
version: '3.9'
services:
app:
build: .
ports:
- "8000:8000"
environment:
# Use the development environment file
ENV_FILE: ".env"
extra_hosts:
- "host.docker.internal:host-gateway"Note:
Theextra_hostsmapping is used so that the container can resolvehost.docker.internal(which points to your host machine) when running on Linux.
In production, you typically point yourDATABASE_URLandDATABASE_HOSTto your cloud database host (using an.env.productionfile), and the extra hosts mapping is not required.
Your environment files control database connection settings:
-
.env (Development)
DATABASE_URL=postgresql://confidential_db_user:password@host.docker.internal:5432/nexux_db DATABASE_HOST=host.docker.internal PROJECT_NAME=true-source
-
.env.production (Production)
DATABASE_URL=postgresql://confidential_db_user:ProdPassword@your-cloud-db-host:5432/nexux_db DATABASE_HOST=your-cloud-db-host PROJECT_NAME=true-source
You can select which file to use by setting the ENV_FILE environment variable (as done in your docker-compose). For production deployments, ensure your orchestration system injects the proper environment variables or loads the correct .env.production file.
- Build and Start Containers (Development):
docker-compose up --build
- Access the API Documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- Ensure
DATABASE_URLis correct in production.
Adjust your.env.productionor environment variables accordingly. - Run migrations before deployment:
alembic upgrade head
- For production, consider using Gunicorn:
gunicorn -k uvicorn.workers.UvicornWorker true_app.main:app --bind 0.0.0.0:8000
| Command | Description |
|---|---|
uvicorn true_app.main:app --reload |
Start FastAPI server (development) |
alembic revision --autogenerate -m "message" |
Create a new migration |
alembic upgrade head |
Apply migrations |
alembic downgrade -1 |
Undo last migration |
psql -U confidential_db_user -d nexux_db -h localhost -c "\dt master_data.*" |
Check tables |
brew services start postgresql@14 |
Start PostgreSQL |