A simple CRUD REST API built using Flask and PostgreSQL, containerized using Docker.
This project demonstrates building, running, and testing backend APIs with database integration.
flask-postgres-crud/
βββ Dockerfile # Dockerfile to containerize the Flask app
βββ app.py # Main Flask application
βββ database/ # PostgreSQL database setup scripts
| βββ Dockerfile # Dockerfile to the containerizeto postgresql
βββ requirements.txt # Python dependencies
βββ venv/ # Python virtual environment
βββ README.md
- Create, Read, Update, Delete (CRUD) operations
- RESTful API design
- PostgreSQL database integration
- Docker containerized (easy deployment)
- Backend: Python 3.11, Flask 2.3.2
- Database: PostgreSQL 15
- Containerization: Docker
git clone https://github.com/samarthgarde/Flask-Postgres-CRUD-API.git
cd Flask-Postgres-CRUD-API
python3 -m venv venv
source venv/bin/activate
docker network create mynetwork
docker volume create pgdata
docker build -t flask-app .
docker run -d --name my-python-container --network mynetwork -e DATABASE_URL=postgresql://postgres:postgres@my_postgres:5432/cruddb -p 5000:5000 flask-app
cd database/
docker build -t postgres:15 .
docker run -d --name my_postgres --network mynetwork -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password -e POSTGRES_DB=cruddb -v pgdata:/var/lib/postgresql/data -p 5432:5432 postgres:15
docker logs my-postgres -f
docker logs my-python-container -f
docker network ls
docker network inspect mynetwork
docker exec -it my-python-container env | grep DATABASE_URL
Test "Get all students" (GET)
- URL:http://localhost:5001/students
- Method: GET
curl http://localhost:5001/students
Test "Add student" (POST)
- URL: http://localhost:5001/students
- Method: POST
- Body (JSON):
{
"firstname": "samarth",
"lastname": "garde",
"birthdate": "2000-01-01",
"email": "samarthgarde@example.com",
"enrolled_date": "2025-09-30"
}
- Using curl:
curl -X POST http://localhost:5001/students \
-H "Content-Type: application/json" \
-d '{"firstname":"samarth",garde":"Doe","birthdate":"2000-01-01","email":"samarthgarde@example.com","enrolled_date":"2025-09-30"}'
Test "Get single student" (GET)
- URL: http://localhost:5001/students/1
- Method: GET
curl http://localhost:5001/students/1
Test "Add item" (POST)
- URL: http://localhost:5001/items
- Body (JSON):
{
"name": "Book",
"description": "Python Programming Book"
}
- usage with curl:
curl -X POST http://localhost:5001/items \
-H "Content-Type: application/json" \
-d '{"name":"Laptop","description":"Gaming laptop"}'
- Get all items
curl http://localhost:5001/items/1
- Update an item
{"id":1,"name":"Laptop Pro","description":"High-end gaming laptop"}
- Delete an item
curl -X DELETE http://localhost:5001/items/1
Step 1: Access Postgres container
docker exec -it my_postgres psql -U postgres -d cruddb
Step 2: Show tables
\dt
Step 3: Select data in a table format
SELECT * FROM students;
SELECT * FROM items;
step 4: To exit psql
\q
-
pgAdmin (official, free)
-
Steps:
-
1.Add a new server
-
Right-click Servers β Create β Serverβ¦
-
General tab:
-
Name: MyDockerPostgres (any name)
-
Connection tab:
-
Host name/address: localhost
-
Port: 5432
-
Username: postgres
-
Password: postgres
-
Save Password: check the box
-
Click Save.
-
Verify connection
-
Expand Servers β MyDockerPostgres β Databases β cruddbβ Schemas β public β Tables
-
You should see your tables: students and items.
View data in table format
- Right-click table (e.g., students) β View/Edit Data β All Rows
- pgAdmin shows data in a spreadsheet-like format.
- You can edit, delete, or insert rows directly in pgAdmin.
- Also, you can run custom SQL queries in Query Tool (Tools β Query Tool)
SELECT * FROM students;
SELECT * FROM items;
This project is intended to:
- Teach Flask + PostgreSQL integration
- Demonstrate CRUD operations with REST API
- Provide practical Docker containerization experience
Made with β€οΈ Samarth Garde