Minimal PostgreSQL utility with psycopg2
Project description
postg.py
A minimal PostgreSQL database utility class using psycopg2 with basic methods for interacting with a PostgreSQL database.
Features
- Connect to PostgreSQL
- Create tables
- Insert records
- Select records (with optional filters)
- Update records (by condition)
- Delete records (by condition)
- Count rows
- Execute raw SQL queries with parameters
Requirements
- Python 3.7+
- psycopg2
- python-dotenv (optional, for loading env variables)
Installation
pip install postg
## Docs
1. Initialize and connect
from postg import DB
db = DB(
dbname="your_db_name",
user="your_user",
password="your_password",
host="localhost",
port="5432"
)
db.connect()
2. Create a table
schema = """
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL
);
"""
db.create_table(schema)
3. Insert data
db.insert("users", {"name": "Alice", "email": "alice@example.com"})
4. Select data
users = db.select("users")
filtered = db.select("users", {"name": "Alice"})
5. Update records
db.update("users", filters={"name": "Alice"}, data={"email": "new@example.com"})
6. Delete records
db.delete("users", {"email": "new@example.com"})
7. Count rows
total = db.count("users")
filtered = db.count("users", {"name": "Bob"})
8. Run raw SQL queries
# Insert
db.query("INSERT INTO users (name, email) VALUES (%s, %s)", ("John", "john@example.com"))
# Select
result = db.query("SELECT * FROM users WHERE name = %s", ("John",))
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
postg_q-0.1.0.tar.gz
(1.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file postg_q-0.1.0.tar.gz.
File metadata
- Download URL: postg_q-0.1.0.tar.gz
- Upload date:
- Size: 1.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
545f1d2e163a5b62d1dd1fe59625cb65301d09df498c290f36ca9776e04f8124
|
|
| MD5 |
b2b0751ed0df7c7cfe2f58a8c7ccf8cb
|
|
| BLAKE2b-256 |
466d67b5521f64fc5d2e91cb2638ce9c67477ac30ada9dd52c1c7661060fddfd
|
File details
Details for the file postg_q-0.1.0-py3-none-any.whl.
File metadata
- Download URL: postg_q-0.1.0-py3-none-any.whl
- Upload date:
- Size: 1.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d2c67db062803059b816f0cec46cd4a1da08664c242c8ffc8d206605b2106296
|
|
| MD5 |
96490d61d40676394545670c61e66d79
|
|
| BLAKE2b-256 |
e7c63938cae5a37df68bd46d6c6f9d0ae7c37901d27fce7862fd37707a4c1214
|