This project is a clean, production-ready database built for pharmaceutical factories. It uses PostgreSQL to automatically track raw materials, handle expiry risks, and manage safe product recalls. With smart automation built right in, it keeps data accurate and ensures strict quality standards without messy manual updates.
A production-ready PostgreSQL database system built for pharmaceutical manufacturing and automated warehouse operations. Designed to meet strict compliance standards, it handles complex inventory and logistics workflows directly inside the database using PL/pgSQL. The system ensures reliable data integrity, safely processes multi-step warehouse transactions, and keeps a secure, tamper-proof audit trail to support FDA compliance—all managed at the database level.
The database architecture is designed to satisfy Boyce-Codd Normal Form (BCNF) to eliminate data redundancy, structured across 5 distinct operational layers:
- Master Registries:
Material_Master,Account_Master,Employee_Master,Equipment_Master,Product_Master. - Financials & Logistics:
Transactions(unified buy/sell accounting) andSupplier_Contract. - Inventory Pipeline:
Warehouse(serialized tracking with unique key pairing),RM_Transaction, andMaterial_Quality_Check. - Production Floor:
Formula_Master,Batch,Material_Dispensing, andProduction_Log(with asynchronous clock validation). - Post-Production Compliance:
Product_Quality_Check,FG_Transaction,Product_Recall, andMaintenance_Log.
Instead of relying on unstable frontend logic, this system is self-defending—using reactive database triggers and atomic transactional scripts to maintain corporate security.
- Mechanism: Listens
AFTER INSERTonMaterial_Dispensing. - Action: Automatically subtracts quantities issued to a batch from raw material storage.
- Fail-Safe: Intercepts the transaction and throws a hard rollback if warehouse stock drops below the requested allocation amount.
- Mechanism: Listens
BEFORE INSERTonFG_Transaction. - Action: Validates the targeted batch against the
Product_Quality_Checklaboratory data registry. - Fail-Safe: Blocks commercial invoicing with custom exceptions if a batch is completely untested (
NULL) or explicitly flagged asFAILED.
- Mechanism: Protects the
BatchtableBEFORE INSERT OR UPDATE. - Action: Restricts temporal anomalies (manufacturing dates cannot exist in the future) and strictly mandates a minimum 6-month product expiration shelf-life window.
- Mechanism: Listens
AFTER UPDATE OR DELETEonProduct_Quality_Check. - Action: Captures and pushes old vs. new grading metrics, the specific system action, and the current session user into an independent, immutable ledger (
QC_Audit_Log). Designed to fulfill FDA 21 CFR Part 11 guidelines by completely isolating logs from cascading deletions.
v_fda_batch_traceability: Pulls an absolute end-to-end trace map of every batch—aggregating raw ingredients used into a single row using string arrays (STRING_AGG), checking lab statuses, and displaying overall market sales distribution.v_inventory_shortage: A real-time purchasing engine dashboard that monitors active storage capacities against reorder boundaries, calculating precise inventory gaps.v_inventory_expiry_risk: A financial liability manager that utilizes dynamic calendar day subtraction math (Exp_Date - CURRENT_DATE) and conditional matrix logic (CASE WHEN) to instantly categorize un-shipped goods intoCRITICAL,WARNING, orSAFEstatuses.
An advanced PL/pgSQL stored procedure designed to cleanly manage multi-table crisis events in a single transaction block.
CALL execute_product_recall('REC-5004-A', 5004, 'CRITICAL: Structural instability noted.');When invoked, this transaction completely automates:
-
Validating asset parameters and checking safety constraints (Qty_Recalled > 0).
-
Logging the incident details into the active regulatory registry (Product_Recall).
-
Dropping available inventory stock directly to 0 inside the Batch table.
-
Forcing lab statuses to RECALLED to instantly wake up the downstream sales block trigger.
Prerequisites PostgreSQL v12 or higher installed.
Clone the repository:
git clone [https://github.com/your-username/PharmaChain-DB.git](https://github.com/your-username/PharmaChain-DB.git)2.Run the initialization script inside your terminal or pgAdmin Query Tool to create the schema, build the logic tables, and populate the seed dataset:
psql -U postgres -d your_database_name -f schema_and_data.sql