This repository contains code for backend of newsletter project in FastAPI
See the code for client side here
python -m venv fastapi-env
.\fastapi-env\Scripts\Activate.ps1
fastapi-env\Scripts\activate.bat
pip install -r requirements.txt
Create sqlite.db
file in root of your project if you want to use sqlite database
or update SQLALCHEMY_DATABASE_URL
in db/database.py
file to use any other local or remote sql database
uvicorn main:app --reload
--reload flag enables live reload as you save files
This will run your server on http://localhost:8000
You can change port by running this command 👇
uvicorn main:app --port PORT_NUMBER
You can use any approach to deploy your FastAPI application and Postgresql database to production either using Docker containers or using any cloud server provider.
I'm using render.com to deploy server as well as DB.
- On render, create a new PostgreSQL instance, and copy the external URL from it after it is created.
- change postgres to postgresql in the url
- add the URL in POSTGRESQL_DATABASE_URL varible in your .env file
- Install these dependencies:
pip install alembic psycopg2 psycopg2-binary
- Create alembic migration environment
alembic init alembic
- Update sqlalchemy.url in alembic.ini file to your postgresql url
- In alembic/env.py file import your Base and all models from your database config files
- Update value of target_metadata to
target_metadata = Base.metadata
- Do initial migration
alembic revision --autogenerate
- You can now start using this database
- Add alembic.ini and alembic/versions in .gitignore
- Now you can directly connect your code repo on render.com and deploy the app in new web service
- Add all the environment variables of your app on render
- For each subsequent migration of your database you can run the same command