This project is a simple CRUD (Create, Read, Update, Delete) API built with Python's Flask framework and PostgreSQL as the database. Created for fun only.
The API operates on a table with the following columns:
- String
- Int
- Primary Key
- Date (two columns)
- Boolean
- Datetime
The API supports the following operations:
- CREATE: Inserts new entries into the table.
- READ: Retrieves all entries from the table.
- UPDATE: Modifies existing entries in the table. Requires the column name and the new value.
- DELETE: Removes entries from the table. Requires either the ID or NAME as a parameter.
To run this project, you need to install the following:
- PostgreSQL: The database used by the API.
- DBeaver (optional): A database management tool.
- Postman: A tool for testing APIs.
- Python: The programming language used to build the API.
You also need to install the following Python libraries:
- Flask: A lightweight web framework.
- Flask-SQLAlchemy: An extension for Flask that adds support for SQLAlchemy.
- SQLAlchemy: A SQL toolkit and ORM.
- Psycopg2: A PostgreSQL adapter for Python.
Prepare for using this project.
-
Install PostgreSQL: Use the following commands to install PostgreSQL and its additional modules:
sudo apt-get update sudo apt-get install postgresql postgresql-contrib
-
Start PostgreSQL service: Start the PostgreSQL service with the following command:
sudo service postgresql start
-
Access PostgreSQL command line: Switch to the
postgres
user and access the PostgreSQL command line:sudo su - postgres psql
-
Create a database and a user: Create a new PostgreSQL user and a new database, and grant all privileges on the database to the user:
CREATE USER angeliav WITH PASSWORD 'Angel14^_^'; CREATE DATABASE flask_example; GRANT ALL PRIVILEGES ON DATABASE flask_example TO angeliav; \q
Then exit the PostgreSQL command line and the
postgres
user:exit
-
Login to the new database and create a table: Login to the new database as the new user and create a table:
psql -h localhost -U angeliav -d flask_example
Then create a
users
table:CREATE TABLE users ( id SERIAL PRIMARY KEY, name VARCHAR(50), created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, is_active BOOLEAN DEFAULT TRUE );
-
Set up a virtual environment: Use the following commands to create and activate a virtual environment:
python3 -m venv env source env/bin/activate # Linux . .\env\Scripts\activate # Powershell
-
Install dependencies: Use the following commands to install the necessary Python libraries:
pip install flask pip install flask_sqlalchemy pip install sqlalchemy pip install psycopg2-binary
Note:
psycopg2-binary
is a stand-alone package used to avoid build dependencies problem which sometimes occur while installingpsycopg2
.