An open API service indexing awesome lists of open source software.

https://github.com/tameronline/postgres-admin-tools

Utility scripts for managing PostgreSQL databases and tables via SQLAlchemy and psycopg2.
https://github.com/tameronline/postgres-admin-tools

Last synced: 9 months ago
JSON representation

Utility scripts for managing PostgreSQL databases and tables via SQLAlchemy and psycopg2.

Awesome Lists containing this project

README

          

# ๐Ÿ› ๏ธ PostgreSQL Admin Tools

![Build Status](https://github.com/TamerOnLine/postgres-admin-tools/actions/workflows/python-app.yml/badge.svg)

A lightweight admin interface and CLI toolkit for PostgreSQL, built with **Flask**, **SQLAlchemy**, and **psycopg2**.

> Manage your databases and tables with ease โ€“ via web or terminal.

---

## ๐Ÿ“ฅ Clone the Repository
To get started, clone this repository to your local machine using Git:

```bash
git clone https://github.com/TamerOnLine/postgres-admin-tools.git
cd postgres-admin-tools
```
- Make sure you have Git installed.
- [You can download it from](https://git-scm.com).


models definitions

๐Ÿ“ธ Cloning the repository via Git command line

---

## ๐Ÿ“ฆ Features

- ๐Ÿ” Secure user system with Flask-Login (default admin: `admin/12345`)
- โš™๏ธ Create or drop PostgreSQL databases
- ๐Ÿงฑ Manage tables (create / migrate / drop) via SQLAlchemy
- ๐Ÿ’พ Full database backup & restore using `pg_dump` / `psql`
- ๐Ÿงฉ Easy environment config via `.env` file
- ๐ŸŒ Flask-based web UI ready out of the box

---

## ๐ŸŽฌ Demo


PostgreSQL Admin Tools demo

๐Ÿ“ฝ๏ธ Quick demo of the PostgreSQL Admin Tools in action

---

## ๐Ÿ”ง Tech Stack

| Technology | Description |
|----------------|-------------------------------------|
| Python | Core programming language |
| PostgreSQL | Relational database engine |
| SQLAlchemy | ORM for database modeling |
| psycopg2 | PostgreSQL driver for Python |
| python-dotenv | Load `.env` variables into runtime |

---

## ๐Ÿงฑ Project Structure

```
postgres-admin-tools/
โ”œโ”€โ”€ myapp.py # Flask app with login & DB panel
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ LICENSE
โ”œโ”€โ”€ README.md
โ””โ”€โ”€ models/
โ”œโ”€โ”€ models_definitions.py # SQLAlchemy models
โ””โ”€โ”€ db_postgres/
โ”œโ”€โ”€ create.py # Create DB if not exists
โ”œโ”€โ”€ drop.py # Drop DB (safe disconnect)
โ”œโ”€โ”€ drop_table.py # Drop individual/all tables
โ”œโ”€โ”€ manage_tables.py # Schema updates
โ”œโ”€โ”€ BACKUP.py # Backup to SQL file
โ”œโ”€โ”€ RESTORE.py # Restore from backup
โ””โ”€โ”€ db_config.py # Load from .env
```

---

## โš™๏ธ Environment Setup

Create a `.env` file in the root directory:

```env
DATABASE_URL=postgresql://user:password@localhost:5432/your_db
SECRET_KEY=your_secret_here
```

---

## ๐Ÿš€ Quick Start
### 1. Create Virtual Environment
```bash
# Windows
py -3.12 -m venv venv
.\venv\Scripts\Activate
```


models definitions

๐Ÿ“ธ Virtual environment activated successfully

---

```bash
# macOS / Linux
python3 -m venv venv
source venv/bin/activate
```

---

### Install Dependencies

```bash
pip install -r requirements.txt
```


models definitions

๐Ÿ“ธ Installing dependencies using pip

---

### Create Database

```bash
py .\models\db_postgres\create.py
```


models definitions

๐Ÿ“ธ Creating PostgreSQL database

---

### Drop Database

```bash
py .\models\db_postgres\drop.py
```


models definitions

๐Ÿ“ธ Dropping the database safely

---

### Drop Tables

```bash
py .\models\db_postgres\drop_table.py
```


models definitions

๐Ÿ“ธ Dropping selected or all tables

---

### Manage Tables

```bash
py .\models\db_postgres\manage_tables.py
```


models definitions

๐Ÿ“ธ CLI interface to manage tables (create or sync)

---

### Backup Database

```bash
py .\models\db_postgres\BACKUP.py
```


models definitions

๐Ÿ“ธ Generating SQL backup file using `pg_dump`

---

### Restore Database

```bash
py .\models\db_postgres\RESTORE.py
```


models definitions

๐Ÿ“ธ Restoring database from SQL file

---

## ๐Ÿ›ก๏ธ User System

The system automatically creates a **default admin user** on first run if no user with username `admin` is found.
- **Username**: `admin`
- **Password**: `12345`

To manually trigger this, run the following command:

```bash
py .\models\models_definitions.py
```
You can modify this logic in [`models/models_definitions.py`](models/models_definitions.py).


models definitions

๐Ÿ“ธ Creating default admin user if not exists

---

## ๐Ÿงช SQLite Test Mode

If `DATABASE_URL` is not set in the `.env`, the system defaults to SQLite (`sqlite:///test.db`) for quick testing.

> Note: Scripts like `create.py`, `BACKUP.py`, etc., require PostgreSQL and do not support SQLite.

---

## ๐Ÿ“‹ CLI Command Summary

| Operation | Script | Mode |
|----------------------|---------------------|-----------------|
| ๐Ÿ—๏ธ Create Database | `create.py` | CLI |
| โŒ Drop Database | `drop.py` | CLI |
| ๐Ÿงน Drop Tables | `drop_table.py` | Interactive CLI |
| ๐Ÿงฉ Manage Tables | `manage_tables.py` | Interactive CLI |
| ๐Ÿ’พ Backup Database | `BACKUP.py` | CLI |
| โ™ป๏ธ Restore from Backup | `RESTORE.py` | Interactive CLI |

---

## ๐Ÿ“„ License

This project is licensed under the MIT License.
See the [LICENSE](./LICENSE) file for details.

---

## ๐Ÿ‘จโ€๐Ÿ’ป Author

**Tamer Hamad Faour**
GitHub: [@TamerOnLine](https://github.com/TamerOnLine)