manutej/luxor-claude-marketplace/psycopg

Claude Code and agent skill by manutej/luxor-claude-marketplace

Trust tier 3
  • community
Source check pending

Checking that the SKILL.md file is still in the repository.

PostgreSQL adapter for Python - customer support tech enablement for database operations, query optimization, and data management

Install

npx vskill@latest install manutej/luxor-claude-marketplace/psycopg

SKILL.md

Psycopg Skill - Customer Support Tech Enablement

Overview

This skill provides comprehensive guidance on using psycopg3 (and psycopg2 where applicable) for PostgreSQL database operations in customer support contexts. It covers everything from basic connections to advanced features like connection pooling, async operations, bulk data transfers, and performance optimization.

Target Audience

  • Customer Support Engineers working with backend systems
  • Technical Support teams handling data migrations
  • Support teams analyzing customer data and usage patterns
  • Backend developers building support tools and dashboards
  • QA engineers writing integration tests for PostgreSQL-backed applications

Core Competencies

When using this skill, you will be able to:

  1. Establish robust database connections with proper error handling and retry logic
  2. Implement connection pooling for high-performance multi-threaded applications
  3. Execute async database operations for non-blocking I/O in modern Python applications
  4. Perform bulk data operations efficiently using COPY and batch insert techniques
  5. Write secure parameterized queries to prevent SQL injection
  6. Handle transactions with proper commit/rollback semantics
  7. Optimize query performance using prepared statements and binary protocols
  8. Work with JSON/JSONB data for flexible customer data storage
  9. Implement retry patterns for resilient database connections
  10. Test database code effectively using pytest and fixtures

Installation and Setup

Installing Psycopg3

For new projects, always use psycopg3:

bash
# Basic installation (pure Python implementation)
pip install psycopg
# With binary package for better performance
pip install "psycopg[binary]"
# With connection pool support
pip install "psycopg[pool]"
# Complete installation with all extras
pip install "psycopg[binary,pool]"
# For C implementation (fastest, requires build tools)
pip install "psycopg[c]"
Installing Psycopg2 (Legacy Projects)

If you're maintaining legacy code:

bash
# Binary package (recommended for most cases)
pip install psycopg2-binary
# Source package (requires PostgreSQL dev libraries)
pip install psycopg2
Environment Setup for Customer Support

Create a .env file for database credentials:

bash
# Production support database (read-only)
SUPPORT_DB_HOST=support-db.company.com
SUPPORT_DB_PORT=5432
SUPPORT_DB_NAME=support_analytics
SUPPORT_DB_USER=support_readonly
SUPPORT_DB_PASSWORD=secure_password_here
# Development/testing database
DEV_DB_HOST=localhost
DEV_DB_PORT=5432
DEV_DB_NAME=support_dev
DEV_DB_USER=dev_user
DEV_DB_PASSWORD=dev_password

Load with python-dotenv:

python
from dotenv import load_dotenv
import os
load_dotenv()
DB_CONFIG = {
'host': os.getenv('SUPPORT_DB_HOST'),
'port': os.getenv('SUPPORT_DB_PORT'),
'dbname': os.getenv('SUPPORT_DB_NAME'),
'user': os.getenv('SUPPORT_DB_USER'),
'password': os.getenv('SUPPORT_DB_PASSWORD'),
}

Connection Management

Basic Connection (Psycopg3)
python
import psycopg
# Using connection string
conn = psycopg.connect(
"postgresql://user:password@localhost:5432/support_db"
)
# Using parameters (recommended for flexibility)
conn = psycopg.connect(
host="localhost",
port=5432,
dbname="support_db",
user="support_user",
password="secure_password",
autocommit=False, # Explicit transaction management
connect_timeout=10 # Timeout in seconds
)
# Always use context manager for automatic cleanup
with psycopg.connect(**DB_CONFIG) as conn:
with conn.cursor() as cur:
cur.execute("SELECT version()")
print(cur.fetchone()[0])
# Connection automatically closed
Connection with Retry Logic

For resilient customer support applications:

python
import psycopg
import time
import logging
from typing import Optional
logger = logging.getLogger(__name__)
def connect_with_retry(
max_attempts: int = 5,
retry_delay: float = 2.0,
backoff_factor: float = 2.0,
**conn_params
) -> Optional[psycopg.Connection]:
"""
Establish database connection with exponential backoff retry.
Args:
max_attempts: Maximum number of connection attempts
retry_delay: Initial delay between retries (seconds)
backoff_factor: Multiplier for exponential backoff
**conn_params: Connection parameters for psycopg.connect()
Returns:
Connection object or None if all attempts fail
"""
attempt = 0
current_delay = retry_delay
while attempt < max_attempts:
try:
conn = psycopg.connect(**conn_params)
logger.info(f"Database connection established on attempt {attempt + 1}")
return conn
except psycopg.OperationalError as e:
attempt += 1
if attempt >= max_attempts:
logger.error(f"Failed to connect after {max_attempts} attempts: {e}")
raise
logger.warning(
f"Connection attempt {attempt} failed: {e}. "
f"Retrying in {current_delay:.1f}s..."
)
time.sleep(current_delay)
current_delay *= backoff_factor
return None
# Usage in customer support tools
try:
conn = connect_with_retry(
host="support-db.company.com",
dbname="support_analytics",
user="support_user",
password=os.getenv("DB_PASSWORD"),
max_attempts=5
)
with conn:
# Perform operations
pass
except psycopg.OperationalError:
logger.critical("Database unavailable - alerting on-call team")
# Send alert to monitoring system

Connection Pooling

Synchronous Connection Pool

Essential for high-performance support tools handling multiple requests:

python
from psycopg_pool import ConnectionPool
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger("psycopg.pool").setLevel(logging.INFO)
# Create a global connection pool
pool = ConnectionPool(
conninfo="postgresql://user:password@localhost/support_db",
min_size=2, # Minimum connections to maintain
max_size=10, # Maximum connections allowed
max_idle=300, # Close idle connections after 5 minutes
max_lifetime=3600, # Recycle connections after 1 hour
timeout=30, # Wait up to 30s for a connection
check=ConnectionPool.check_connection, # Validate connections
open=True # Open pool immediately
)
# Wait for minimum connections to be established
pool.wait()
# Use in application code
def get_customer_tickets(customer_id: int):
"""Fetch all tickets for a customer."""
with pool.connection() as conn:
with conn.cursor() as cur:
cur.execute(
"SELECT ticket_id, subject, status, created_at "
"FROM tickets WHERE customer_id = %s "
"ORDER BY created_at DESC",
(customer_id,)
)
return cur.fetchall()
# Monitor pool health
def check_pool_health():
"""Get pool statistics for monitoring."""
stats = pool.get_stats()
return {
'pool_size': stats.get('pool_size', 0),
'available': stats.get('pool_available', 0),
'waiting': stats.get('requests_waiting', 0),
'requests_total': stats.get('requests_num', 0),
'requests_queued': stats.get('requests_queued', 0),
'connection_errors': stats.get('connections_errors', 0)
}
# Cleanup on application shutdown
import atexit
atexit.register(pool.close)
Asynchronous Connection Pool

For async FastAPI/Starlette support applications:

python
from psycopg_pool import AsyncConnectionPool
from contextlib import asynccontextmanager
from fastapi import FastAPI
import logging
logger = logging.getLogger(__name__)
# Create async pool
async_pool = AsyncConnectionPool(
conninfo="postgresql://user:password@localhost/support_db",
min_size=5,
max_size=20,
open=False # Open during application startup
)
# FastAPI integration with lifespan
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Manage async pool lifecycle with FastAPI."""
await async_pool.open()
logger.info("Async connection pool opened")
yield
await async_pool.close()
logger.info("Async connection pool closed")
app = FastAPI(lifespan=lifespan)
# Async endpoint using the pool
@app.get("/api/customers/{customer_id}/tickets")
async def get_customer_tickets(customer_id: int):
"""Fetch customer tickets asynchronously."""
async with async_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"SELECT ticket_id, subject, status, priority, created_at "
"FROM tickets "
"WHERE customer_id = %s "
"ORDER BY created_at DESC "
"LIMIT 100",
(customer_id,)
)
tickets = await cur.fetchall()
return [
{
'ticket_id': t[0],
'subject': t[1],
'status': t[2],
'priority': t[3],
'created_at': t[4].isoformat()
}
for t in tickets
]
# Health check endpoint
@app.get("/health/database")
async def database_health():
"""Check database and pool health."""
try:
async with async_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute("SELECT 1")
result = await cur.fetchone()
stats = async_pool.get_stats()
return {
'status': 'healthy',
'database_responsive': result[0] == 1,
'pool_stats': {
'size': stats.get('pool_size'),
'available': stats.get('pool_available'),
'waiting': stats.get('requests_waiting')
}
}
except Exception as e:
logger.error(f"Database health check failed: {e}")
return {'status': 'unhealthy', 'error': str(e)}

Async Operations

Basic Async Pattern
python
import asyncio
import psycopg
async def fetch_support_metrics():
"""Fetch support metrics asynchronously."""
async with await psycopg.AsyncConnection.connect(
"postgresql://user:password@localhost/support_db"
) as conn:
async with conn.cursor() as cur:
# Execute query asynchronously
await cur.execute(
"""
SELECT
status,
COUNT(*) as count,
AVG(response_time_minutes) as avg_response
FROM tickets
WHERE created_at > CURRENT_DATE - INTERVAL '7 days'
GROUP BY status
"""
)
# Fetch results
results = await cur.fetchall()
# Process asynchronously
metrics = {}
async for row in cur:
metrics[row[0]] = {
'count': row[1],
'avg_response': float(row[2]) if row[2] else 0
}
return metrics
# Run async function
if __name__ == "__main__":
metrics = asyncio.run(fetch_support_metrics())
print(metrics)
Concurrent Async Queries

Execute multiple queries concurrently for better performance:

python
import asyncio
import psycopg
from typing import List, Tuple
async def fetch_customer_data(customer_id: int, conn: psycopg.AsyncConnection):
"""Fetch all data for a customer."""
async with conn.cursor() as cur:
await cur.execute(
"SELECT * FROM customers WHERE customer_id = %s",
(customer_id,)
)
return await cur.fetchone()

This is the start of the file. Read the full SKILL.md on GitHub.

Security

  • Security scanPassed, 95 of 100Pattern checks on the skill's files.
  • Model reviewPassed

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

Repository stars
54
Repository forks
15
7-day trend score
20

Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 5 May 2026, 6 months ago.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Cursor
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/manutej/luxor-claude-marketplace/psycopg/badge)](https://verified-skill.com/skills/manutej/luxor-claude-marketplace/psycopg)

Something wrong with this skill or its page? Tell us.