Minimal PostgreSQL utility with psycopg2
Project description
postg.py
https://pypi.org/project/postg-q/0.1.0/
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.11+
- 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.1.tar.gz
(3.4 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.1.tar.gz.
File metadata
- Download URL: postg_q-0.1.1.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd20419ed8aa1e02a0a381a529f46d3304a990e0ce063f5c698692fb47593472
|
|
| MD5 |
9d77c11c18d1cd503ac4df0a4f2de729
|
|
| BLAKE2b-256 |
1aea91574e20e27634044f7d289903154a47f8c5eb92ff3164605cfb945ef28c
|
File details
Details for the file postg_q-0.1.1-py3-none-any.whl.
File metadata
- Download URL: postg_q-0.1.1-py3-none-any.whl
- Upload date:
- Size: 3.5 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 |
24d999da931e791607de8dfc29c3a12bc13723df0ab77f6d754b6c1616b2519b
|
|
| MD5 |
06db1a289bd7e32a6b8e2d82d24a45ee
|
|
| BLAKE2b-256 |
3c414d87925fe2be5c4e91a11cafc604c4a290f229341c441f88f2c73afc5a02
|