This is a sample FastAPI CRUD application that uses a PostgreSQL database.
- Python 3.8 or higher
- PostgreSQL 12 or higher
git clone https://github.com/Athul-dev-7/fastAPI.git
cd fastAPI
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
-
To install PostgreSQL on Ubuntu, you can follow these steps:
-
-
Update the package manager's package list:
sudo apt update
-
-
-
Install the PostgreSQL package:
sudo apt install postgresql postgresql-contrib
This will install PostgreSQL and some additional utilities.
-
-
-
After the installation is complete, the PostgreSQL service will be started automatically. You can check the status of the service by running:
systemctl status postgresql
-
-
-
By default, PostgreSQL creates a user named
postgres
with the rolepostgres
. You can connect to the PostgreSQL server as this user by running:sudo su - postgres
-
-
- Once you are connected to the PostgreSQL server, you can create new user and database.
-
-
ADD .env file at the root of the project directory and edit these fields according to your database config:
- DATABASE_HOST_NAME=
- DATABASE_PORT=
- DATABASE_NAME=
- DATABASE_USERNAME=
- DATABASE_PASSWORD=
- SECRET_KEY=
- ALGORITHM=
- ACCESS_TOKEN_EXPIRE_MINUTES=
-
Run the following command to apply the database migrations:
alembic upgrade head
uvicorn app.main:app --reload
The application will be available at http://localhost:8000.
pytest
coverage report -m
- Docker
- Docker Compose
-
Once installed the above 2 requirements, go to the project's root directory containing both Dockerfile & docker-compose.yaml files and run this command:
sudo docker-compose up --build -d
-
Once container is up, Please follow these commands
-
To list the running containers:
sudo docker-compose ps
-
To migrate database using alembic
-
sudo docker-compose exec -it api bash
-
alembic upgrade head
-
-
To down the container
docker-compose down
to down the container