Skip to content

samarthgarde/Flask-Postgres-CRUD-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

17 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Flask + PostgreSQL + Docker CRUD REST API

Python Flask PostgreSQL Docker

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.


πŸ“‚ Project Structure

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

⚑ Features

  • Create, Read, Update, Delete (CRUD) operations
  • RESTful API design
  • PostgreSQL database integration
  • Docker containerized (easy deployment)

πŸ›  Tech Stack

  • Backend: Python 3.11, Flask 2.3.2
  • Database: PostgreSQL 15
  • Containerization: Docker

πŸ“¦ Setup & Run

Clone Repo

git clone https://github.com/samarthgarde/Flask-Postgres-CRUD-API.git
cd Flask-Postgres-CRUD-API

Virtual environment set and activate it

python3 -m venv venv
source venv/bin/activate

Create network

docker network create mynetwork

Create volume

docker volume create pgdata

Build & Run my-webapp container

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

Build & Run postgres:15 container

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

Check live logs

docker logs my-postgres -f
docker logs my-python-container -f

Network Check

docker network ls
docker network inspect mynetwork

Environment Variable Check (DATABASE_URL)

docker exec -it my-python-container env | grep DATABASE_URL

Test APIs using Postman / Curl / Browser (for GET requests)

Test "Get all students" (GET)

curl http://localhost:5001/students

Test "Add student" (POST)

{
  "firstname": "samarth",
  "lastname": "garde",
  "birthdate": "2000-01-01",
  "email": "samarthgarde@example.com",
  "enrolled_date": "2025-09-30"
}

image

  • 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)

curl http://localhost:5001/students/1

Test "Add item" (POST)

{
  "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

Using psql in terminal (Postgres CLI)

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

Using a GUI tool

  • 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;

image


🎯 Objective

This project is intended to:

  • Teach Flask + PostgreSQL integration
  • Demonstrate CRUD operations with REST API
  • Provide practical Docker containerization experience

πŸ‘¨β€πŸ’» Author

Made with ❀️ Samarth Garde

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published