Skip to main content

A lightweight PostgreSQL wrapper for Python with connection pooling

Project description

znpg

A lightweight PostgreSQL wrapper for Python with connection pooling and a clean API.

Why znpg?

Working with psycopg directly involves a lot of boilerplate. znpg removes the repetition while maintaining the flexibility of raw SQL when you need it.

Key features:

  • Built-in connection pooling
  • Simple CRUD operations
  • Bulk insert support
  • Transaction management
  • SQL injection protection
  • Type hints throughout

Installation

pip install znpg

Quick Start

from znpg import Database

# Connect to database
db = Database()
db.url_connect("postgresql://user:password@localhost:5432/dbname")

# Create table
db.create_table('users', {
    'id': 'SERIAL PRIMARY KEY',
    'name': 'VARCHAR(100) NOT NULL',
    'email': 'VARCHAR(255) UNIQUE',
    'created_at': 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP'
})

# Insert data
db.insert('users', {
    'name': 'John Doe',
    'email': 'john@example.com'
})

# Query data
users = db.select('users', where={'name': 'John Doe'})
print(users)  # [{'id': 1, 'name': 'John Doe', 'email': 'john@example.com', ...}]

# Close connection
db.close()

Using Context Manager

The recommended way to use znpg is with a context manager, which automatically handles connection cleanup:

from znpg import Database

with Database() as db:
    db.url_connect("postgresql://user:password@localhost:5432/dbname")
    
    # Your database operations
    users = db.select('users')
    
# Connection pool automatically closed

Core Operations

Connecting

Using URL string:

db = Database()
db.url_connect("postgresql://user:password@localhost:5432/dbname")

Using individual parameters:

db = Database()
db.manual_connect(
    username="user",
    password="password",
    host="localhost",
    db_name="dbname",
    port=5432
)

Selecting Data

Select all:

users = db.select('users')

Select with conditions:

users = db.select('users', where={'active': True})

Select specific columns:

users = db.select('users', columns=['name', 'email'])

With ordering and limit:

users = db.select('users', 
    where={'active': True},
    order_by='created_at DESC',
    limit=10
)

Inserting Data

Single row:

db.insert('users', {
    'name': 'Jane Smith',
    'email': 'jane@example.com'
})

Multiple rows (bulk insert):

db.bulk_insert('users', [
    {'name': 'Alice', 'email': 'alice@example.com'},
    {'name': 'Bob', 'email': 'bob@example.com'},
    {'name': 'Charlie', 'email': 'charlie@example.com'}
])

Bulk insert is significantly faster for large datasets.

Updating Data

Update with conditions:

db.update('users',
    data={'active': False},
    conditions={'email': 'john@example.com'}
)

Update all rows (requires explicit permission):

db.update('users',
    data={'verified': True},
    allow_all=True
)

Deleting Data

Delete with conditions:

db.delete('users', conditions={'active': False})

Delete all (requires explicit permission):

db.delete('users', allow_deleteall=True)

Table Management

Create Table

db.create_table('products', {
    'id': 'SERIAL PRIMARY KEY',
    'name': 'VARCHAR(200) NOT NULL',
    'price': 'DECIMAL(10, 2)',
    'stock': 'INTEGER DEFAULT 0',
    'created_at': 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP'
})

Drop Table

db.drop_table('old_table', allow_action=True)

With cascade:

db.drop_table('parent_table', cascade=True, allow_action=True)

Check if Table Exists

if db.table_exists('users'):
    print("Table exists")

Get Table Columns

columns = db.get_table_columns('users')
print(columns)  # ['id', 'name', 'email', 'created_at']

Truncate Table

db.truncate('logs')

Utility Methods

Count Rows

total_users = db.count('users')
active_users = db.count('users', where={'active': True})

Check if Record Exists

exists = db.exists('users', {'email': 'john@example.com'})

Get by ID

user = db.get_by_id('users', 'id', 123)

Transactions

For operations that need to be atomic:

with db.transaction() as conn:
    cursor = conn.cursor()
    cursor.execute("UPDATE accounts SET balance = balance - 100 WHERE id = 1")
    cursor.execute("UPDATE accounts SET balance = balance + 100 WHERE id = 2")
    # Automatically commits on success, rolls back on error

Raw SQL

When you need full control, you can execute raw SQL:

# Query with results
results = db.query("SELECT * FROM users WHERE age > %s", [18])

# Execute without results
rows_affected = db.execute("DELETE FROM logs WHERE created_at < %s", ['2023-01-01'])

# Fetch single row
user = db.fetch_one("SELECT * FROM users WHERE id = %s", [123])

Safety Features

znpg includes safety checks for destructive operations:

UPDATE without WHERE clause:

# This will raise ValueError
db.update('users', {'active': False})

# Must explicitly allow
db.update('users', {'active': False}, allow_all=True)

DELETE without WHERE clause:

# This will raise ValueError
db.delete('users')

# Must explicitly allow
db.delete('users', allow_deleteall=True)

DROP TABLE requires confirmation:

# This will raise AuthorizationError
db.drop_table('important_table')

# Must explicitly allow
db.drop_table('important_table', allow_action=True)

Connection Pooling

znpg uses connection pooling by default (1-10 connections). This means:

  • Connections are reused across operations
  • Better performance under load
  • Automatic connection management
  • Thread-safe operations

You don't need to manage connections manually - the pool handles everything.

Requirements

  • Python 3.7+
  • psycopg 3.0+
  • psycopg-pool 3.0+

Performance

Bulk insert performance test (69 rows):

  • Traditional loop insert: ~15-20 seconds
  • znpg bulk_insert: <5 seconds

For data pipelines and ETL operations, bulk_insert provides significant performance improvements.

Error Handling

All methods include error handling and return sensible defaults:

# Returns empty list on error
users = db.select('nonexistent_table')  # []

# Returns False on error
success = db.insert('users', {'invalid': 'data'})  # False

# Returns 0 on error
count = db.count('nonexistent_table')  # None

Errors are printed to console for debugging.

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome. Please open an issue first to discuss proposed changes.

Author

Built by Zain, a 17-year-old developer from Pakistan.

Changelog

Version 1.0.0

  • Initial release
  • Core CRUD operations
  • Connection pooling
  • Bulk insert support
  • Table management
  • Transaction support
  • Safety checks for destructive operations

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

znpg-1.0.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

znpg-1.0.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file znpg-1.0.0.tar.gz.

File metadata

  • Download URL: znpg-1.0.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for znpg-1.0.0.tar.gz
Algorithm Hash digest
SHA256 d6364ecc09c336d7d34d161b081b9e55b89fd392e72a147a4bf714524d02879a
MD5 6d9923ff43ed65b4506961b072b87c75
BLAKE2b-256 a001e1c9f239069918bef3d6a1a6e94306d6f824270acdbaccfe7ec4ef0f6471

See more details on using hashes here.

Provenance

The following attestation bundles were made for znpg-1.0.0.tar.gz:

Publisher: publish.yml on thezn0x/znpg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file znpg-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: znpg-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for znpg-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7e0a85d0950620f2bc8c43b29a3a11b7d26260bfa59fd53746db580ecd06ee37
MD5 c329509d3532605bd1440bdf3dd20830
BLAKE2b-256 9b04801d5237d719a77634bbdc21a73fa5bc79045f25d5a454dce78aa946b99e

See more details on using hashes here.

Provenance

The following attestation bundles were made for znpg-1.0.0-py3-none-any.whl:

Publisher: publish.yml on thezn0x/znpg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page