Generate SVG Entity Relationship Diagrams from PostgreSQL database dump files
Project description
pypgsvg - Lightweight PostgreSQL ERD Generator
pypgsvg is a lightweight Python tool that generates a JS/CSS/SVG interactive Entity Relationship Diagrams (ERDs) from PostgreSQL schema dump files. With only Graphviz as a dependency, manually run or place in CI/CD pipelines, fast rapid deployment verification scenarios.
๏ฟฝ๏ธ Screenshots & Examples
๐ Complete ERD Example
Example of a complex database schema with multiple relationships and interactive features
๐ฌ Interactive Features Demo
Live demonstration of drag, resize, and navigation features
๐บ๏ธ Miniature Overview Navigation
Interactive minimap for navigating large schemas with viewport indicator
๐ Metadata Information Panel
Comprehensive schema statistics and generation parameters
๐ฏ Selection & Details Panel
View detailed SQL for selected tables, foreign keys, and triggers
๐ Basic Schema Example
Simple schema showing core functionality and clean output
๏ฟฝ๐ Enterprise Features
- Zero-dependency Python tool (except Graphviz)
- Interactive SVG output with navigation and selection tools
- Scriptable and automatable for enterprise workflows
- Quick deployment - install and run in seconds
- Self-contained - no database connections required
- Cross-platform support (Windows, macOS, Linux)
โจ Interactive Features
๐ฏ Selection & Navigation Tools
- Smart table/edge selection - Click any element to view detailed SQL
- Miniature overview with viewport indicator for large schemas
- Drag & drop containers - Reposition windows anywhere
- Resizable panels - Customize your workspace layout
๐ Copy & Export Tools
- One-click copy - Copy table definitions, foreign keys, or trigger SQL
- Download selection details - Export selected elements as formatted text
- Emoji-free output - Enterprise-friendly plain text exports
๐ฑ๏ธ Interactive Controls
- Minimize/maximize any panel to focus on your work
- Close buttons for distraction-free viewing
- Pan and zoom with mouse or miniature navigator
- Keyboard shortcuts (ESC/R to reset view)
๐จ Visual Enhancements
- Color-coded tables with accessible contrast
- Hover effects for better element identification
- Professional styling suitable for documentation and presentations
๐ฆ Installation
Lightweight setup - only 2 steps:
-
Install
pypgsvg:pip install pypgsvg
-
Install Graphviz (the only external dependency):
- macOS:
brew install graphviz - Ubuntu/Debian:
sudo apt-get install graphviz - CentOS/RHEL:
sudo yum install graphviz - Windows: Download from Graphviz.org
- macOS:
That's it! Ready for enterprise deployment.
๐ ๏ธ Enterprise Usage
Quick Start - Schema Analysis
# Generate interactive ERD from schema dump
pypgsvg schema.dump --output database_erd --view
# Enterprise automation (CI/CD ready)
pypgsvg Samples/complex_schema.dump --output Samples/complex_schema --rankdir LR --node-sep 4
Advanced Enterprise Options
# Large schema optimization
pypgsvg Samples/complex_schema.dump --output Samples/complex_schema --rankdir LR --node-sep 4
source venv/bin/activate && python -m src.pypgsvg Samples/complex_schema.dump --node-shape=ellipse --show-standalone=false --output=./Samples/complex_schema --rankdir LR --node-sep 2 --packmode
# Custom layout for documentation
pypgsvg schema.dump --rankdir LR --fontsize 20 --node-fontsize 16 --output presentation_erd
Usage
Get Your PostgreSQL Schema
If you don't have a schema dump, generate one with pg_dump:
# Standard schema export (most common)
pg_dump -h your-host -d database -U username -s --no-owner --no-privileges > schema.dump
# Comprehensive export with triggers and functions
pg_dump -h your-host -d database -U username -s -O -F plain --disable-triggers --encoding=UTF8 > schema.dump
Or use our sample schema for testing.
Interactive ERD Generation
Basic usage:
pypgsvg schema.dump --output my_database_erd --view
Enterprise production:
pypgsvg schema.dump \
--output docs/database_architecture \
--rankdir TB \
--node-sep 4 \
--packmode graph \
--rank-sep 3 \
--hide-standalone
The generated SVG includes:
- ๐ฑ๏ธ Interactive selection - Click tables/edges to view SQL details
- ๐ฑ Miniature navigator - Overview panel for large schemas
- ๐ Copy/download tools - Export selected elements
- ๐จ Resizable panels - Customize your workspace
- โจ๏ธ Keyboard shortcuts - ESC/R to reset view
Note: For full interactivity, open the SVG file locally in your browser. GitHub restricts JavaScript for security.
๐ฌ Quick Demo
[๐ฏ View Interactive Example]
[ REFERENCES public.franchises(id)
ON DELETE CASCADE;
๐ข Enterprise Deployment
CI/CD Integration
# GitHub Actions example
- name: Generate Database Documentation
run: |
pip install pypgsvg
sudo apt-get install graphviz
pypgsvg schemas/production.dump --output docs/database_erd
# Docker deployment
FROM python:3.9-slim
RUN apt-get update && apt-get install -y graphviz && rm -rf /var/lib/apt/lists/*
RUN pip install pypgsvg
COPY schema.dump .
RUN pypgsvg schema.dump --output database_diagram
Automation Scripts
#!/bin/bash
# Enterprise schema documentation automation
DATE=$(date +%Y%m%d)
pg_dump -h $DB_HOST -d $DB_NAME -U $DB_USER -s > schema_$DATE.dump
pypgsvg schema_$DATE.dump --output docs/database_erd_$DATE --hide-standalone
echo "โ
Database documentation updated: docs/database_erd_$DATE.svg"
๐ง Complete Feature Overview
๐ Database Schema Parsing
pypgsvg supports comprehensive PostgreSQL schema parsing including:
Table Structure Analysis
- CREATE TABLE statements with full column definitions
- Data type detection for all PostgreSQL types (SERIAL, JSONB, arrays, custom types)
- Column constraints (NOT NULL, DEFAULT values, CHECK constraints)
- Primary key identification (single and composite keys)
- Quoted identifiers and schema-qualified table names
- Unicode support with proper UTF-8 encoding handling
Relationship Mapping
- Foreign key constraints from ALTER TABLE statements
- Inline REFERENCES declarations within CREATE TABLE
- Cascading options (ON DELETE CASCADE, ON UPDATE RESTRICT, etc.)
- Complex relationship patterns (self-referencing, many-to-many)
- Cross-schema references with schema qualification
Advanced SQL Features
- Database triggers (BEFORE/AFTER/INSTEAD OF with INSERT/UPDATE/DELETE)
- Trigger function calls with parameter parsing
- Constraint naming and organization
- Index definitions (when present in dump)
- Sequence relationships for SERIAL columns
๐จ Visual Representation
Intelligent Layout Engine
- Graphviz integration with optimized parameter passing
- Automatic table positioning to minimize edge crossings
- Hierarchical layouts showing data flow and dependencies
- Compact arrangements for large schemas
- Custom spacing controls for readability
Color-Coded Organization
- Deterministic color assignment based on table names
- WCAG-compliant contrast for accessibility
- Color-blind friendly palette selection
- Saturation controls for visual emphasis
- Automatic text color calculation for optimal readability
Interactive Enhancement
- Clickable elements for detailed SQL view
- Hover effects for element identification
- Drag-and-drop interface elements
- Resizable panels for workspace customization
- Keyboard navigation (ESC, R keys for reset)
๐ฅ๏ธ User Interface Components
Selection & Detail Panel
- Multi-select capability for tables and relationships
- SQL source display with proper formatting
- Clipboard integration for easy copying
- Download functionality for formatted exports
- Emoji-free output option for enterprise use
- Trigger information display with execution details
Miniature Navigation
- Schema overview with proportional scaling
- Viewport indicator showing current view
- Click-to-navigate functionality
- Drag viewport for precise positioning
- Zoom level awareness and synchronization
Metadata Information
- Schema statistics (table count, column count, relationship count)
- Generation parameters display
- File information (size, modification date, encoding)
- Processing timestamps for audit trails
- Parameter documentation for reproducibility
๐ ๏ธ Enterprise Integration Features
Automation-Friendly Design
- Command-line interface with extensive options
- Scriptable Python API for programmatic use
- Error handling with detailed reporting
- Return codes for CI/CD integration
- Logging support for monitoring and debugging
Output Customization
- SVG format for web integration and scaling
- Self-contained files with embedded styles and scripts
- No external dependencies in generated output
- Cross-browser compatibility (Chrome, Firefox, Safari, Edge)
- Print-friendly layouts with proper scaling
Quality Assurance
- Input validation with comprehensive error reporting
- Graceful degradation for partial schema parsing
- Memory efficiency for large schema processing
- Performance optimization for quick generation
- Deterministic output for version control
๐ Table Filtering & Exclusion
Automatic Exclusions
pypgsvg intelligently excludes common utility tables:
| Pattern | Examples | Reason |
|---|---|---|
vw_*, *_view |
vw_users, summary_view |
Database views |
*_temp, *_tmp, temp_* |
data_temp, tmp_import |
Temporary tables |
*_bk, *_backup, *_old |
users_bk, data_backup |
Backup tables |
*_log, *_audit, audit_* |
error_log, audit_users |
Logging tables |
*_dups, *_duplicates |
data_dups, user_duplicates |
Cleanup tables |
*_archive, archive_* |
old_archive, archive_2023 |
Archive tables |
Standalone Table Handling
- Configurable display of tables without foreign key relationships
- Useful for utility tables and lookup tables
- Reduces clutter in complex schemas
- Maintains referential integrity in relationship mapping
๐งช Testing & Quality
Enterprise-grade testing with comprehensive coverage:
# Run full test suite (no dependencies required)
python -m pytest tests/tests/
# Generate detailed coverage report
python -m pytest tests/tests/ --cov=src --cov-report=html
open htmlcov/index.html
Quality metrics:
- โ 95%+ code coverage
- โ 70+ comprehensive tests
- โ Enterprise PYTHONPATH support
- โ Cross-platform compatibility
๐๏ธ Architecture & Performance
Lightweight Design
๐ฆ pypgsvg/
โโโ ๐ Pure Python core (~450 lines)
โโโ ๐จ CSS styling (~200 lines)
โโโ โก JavaScript interactivity (~2000 lines)
โโโ ๐งช Comprehensive tests (~1000+ lines)
โโโ ๐ Zero runtime dependencies (except Graphviz)
Performance characteristics:
- Fast parsing - Processes large schemas in seconds
- Memory efficient - Minimal footprint for enterprise deployment
- Scalable output - Handles schemas with hundreds of tables
- Quick startup - No database connections or heavy frameworks
Enterprise-Ready Features
- ๐ Security-focused - No network requirements, processes local files only
- ๐ Audit-friendly - Deterministic output for version control
- ๐ Container-ready - Minimal Docker image size
- โ๏ธ Configurable - Extensive customization options
- ๐ Monitoring - Built-in error reporting and validation
โ๏ธ Configuration & Customization
Advanced Layout Options
# Horizontal layout for wide displays
pypgsvg schema.dump --rankdir LR --node-sep 3 --rank-sep 2
# Compact layout for presentations
pypgsvg schema.dump --packmode graph --fontsize 16 --node-fontsize 14
# Large schema optimization
pypgsvg schema.dump --hide-standalone --esep 8 --rank-sep 4
Table Filtering (Automatic)
Enterprise-focused exclusions for cleaner diagrams:
- Views (
vw_*) - Database views - Backup tables (
*_bk,*_backup) - Temporary backup data - Utility tables (
*_temp,*_tmp) - Temporary processing tables - Log tables (
*_log,*_audit) - Audit and logging tables - Duplicate tables (
*_dups,*_duplicates) - Data cleanup tables - Version tables (
*_old,*_archive) - Historical data tables
Color & Accessibility
- WCAG-compliant color palette with proper contrast ratios
- Automatic text color calculation for readability
- Color-blind friendly palette selection
- High-contrast mode for professional presentations
๐ง Supported Database Features
SQL Parsing Capabilities
- โ CREATE TABLE statements with all PostgreSQL data types
- โ ALTER TABLE constraints and foreign keys
- โ Triggers with BEFORE/AFTER/INSTEAD OF events
- โ Primary keys and unique constraints
- โ Complex data types (JSON, arrays, custom types)
- โ Quoted identifiers and schema-qualified names
- โ Unicode support with proper encoding handling
Advanced SQL Features
-- Fully supported constructs
CREATE TABLE "complex_table" (
id SERIAL PRIMARY KEY,
data JSONB NOT NULL,
tags TEXT[],
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Trigger support
CREATE TRIGGER update_modified_time
BEFORE UPDATE ON complex_table
FOR EACH ROW EXECUTE FUNCTION update_timestamp();
-- Foreign key variations
ALTER TABLE orders
ADD CONSTRAINT fk_customer
FOREIGN KEY (customer_id)
REFERENCES customers(id)
ON DELETE CASCADE ON UPDATE RESTRICT;
๐จ Error Handling & Reliability
Production-ready error management:
- Graceful degradation - Continues processing despite individual parsing errors
- Detailed error reporting - Specific line numbers and context
- Encoding resilience - Handles various character encodings
- Malformed SQL recovery - Attempts to extract partial information
- Validation checks - Ensures output integrity
Enterprise logging:
# Built-in error collection for monitoring
tables, foreign_keys, triggers, errors = parse_sql_dump(sql_content)
if errors:
for error in errors:
log.warning(f"Schema parsing issue: {error}")
# Continue with partial results
๐ค Contributing
We welcome enterprise users and contributors:
- Code quality - Follow PEP 8 and maintain >95% test coverage
- Enterprise focus - Consider automation and deployment scenarios
- Performance - Optimize for large schemas and CI/CD usage
- Documentation - Update examples for enterprise use cases
- Testing - Add tests for new SQL patterns and edge cases
๐ Dependencies
Minimal dependency footprint for enterprise deployment:
Required
- Python 3.8+ (standard in most enterprise environments)
- Graphviz (system package, widely available)
Development/Testing Only
pytest>=7.0.0- Testing frameworkpytest-cov>=4.0.0- Coverage reporting
That's it! No heavy frameworks, databases, or complex runtime dependencies.
๐ License & Enterprise Usage
This project is released under the MIT License, making it suitable for:
- โ Commercial use in enterprise environments
- โ Modification for internal requirements
- โ Distribution within organizations
- โ Private use without attribution requirements
Perfect for enterprise adoption with minimal legal overhead.
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
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 pypgsvg-1.1.2a0.tar.gz.
File metadata
- Download URL: pypgsvg-1.1.2a0.tar.gz
- Upload date:
- Size: 281.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4085a0922596cafae7a60eeaaa25c56276dc6db06aca450057b8959dc8cd0012
|
|
| MD5 |
6578856f4679224ce7565b81e8172b5b
|
|
| BLAKE2b-256 |
c3cd85ac5ff2c7ac23c6d59c135bd0ebbed0fe030505ece78d754026ef1fd791
|
File details
Details for the file pypgsvg-1.1.2a0-py3-none-any.whl.
File metadata
- Download URL: pypgsvg-1.1.2a0-py3-none-any.whl
- Upload date:
- Size: 28.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
951b6455658315d7c825b6d59dbdc913ea6b72ae79e9e5fddb9332f1c75cee3f
|
|
| MD5 |
38b77d5a4d4c6113957566b11deb78f0
|
|
| BLAKE2b-256 |
8b6a1d2a2dc49eeb37e2944df2d67ef35d791a30b9def95292dfa8d814b815b3
|