A simple console-based CRUD application written in C++ using libpq (the PostgreSQL C API).
The database runs via Docker Compose, and the project builds using CMake.
- Create, Read, Update, and Delete student data
- Uses PostgreSQL as the database
- Environment configuration via
.envfile - Quick setup with Docker Compose
- Input validation and simple error handling
.
├── .env.example # Example environment variables
├── .gitignore # Git ignore file
├── CMakeLists.txt # CMake build configuration
├── crud_data_mahasiswa.cpp # Main C++ source code
└── docker-compose.yml # PostgreSQL Docker setup
git clone https://github.com/KaivanKeren/simple-crud.git
cd simple-crudCopy the example file and update it:
cp .env.example .envExample .env:
DB_HOST=localhost
DB_PORT=5439
DB_NAME=cpp_db
DB_USER=user_cpp
DB_PASS=password_cppdocker-compose up -dCheck containers:
docker psConnect to PostgreSQL:
docker exec -it cpp_db psql -U user_cpp -d cpp_dbCreate the mahasiswa table:
CREATE TABLE mahasiswa (
id SERIAL PRIMARY KEY,
nim VARCHAR(20) NOT NULL,
nama VARCHAR(100) NOT NULL,
jurusan VARCHAR(100),
umur INT
);Linux/macOS:
cmake -S . -B build && cmake --build build && ./build/crud_data_mahasiswa=== MENU CRUD Data Mahasiswa ===
1. Tambah Data
2. Tampilkan Data
3. Ubah Data
4. Hapus Data
5. Keluar
Example:
Masukkan Nama: Ismail
Masukkan NIM: 230102011
Masukkan Jurusan: Information System
Masukkan Umur: 18
✅ Data berhasil ditambahkan!
Make sure PostgreSQL client library is installed.
Ubuntu/Debian:
sudo apt install libpq-devFedora:
sudo dnf install postgresql-develHere’s a recommended .gitignore for this setup:
# CMake build files
/build/
CMakeCache.txt
CMakeFiles/
Makefile
# Environment file
.env
# Logs and temp files
*.log
*.out
- Commit
.env.example, but don’t commit the actual.envfile. - You can create a
docker-compose.override.ymlfor local testing (add it to.gitignore). - If you can’t connect to PostgreSQL, check the
.envvalues and port mapping.
MIT License — free to use, modify, and learn from.