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.
๐ฆ Installation
Lightweight setup - only 2 steps:
-
Install
pypgsvg:pip install pypgsvg
-
Install Graphviz (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 deployment.
๐ฌ Quick Demo
[๐ฏ View Interactive Example]
Try it yourself:
# Download sample and generate interactive ERD
wget https://github.com/blackburnd/pypgsvg/raw/main/Samples/complex_schema.dump
pypgsvg schema.dump --output demo_erd --view
๐ ๏ธ 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
Usage:
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.
Scriptable API
Perfect for automation and enterprise workflows:
from pypgsvg import parse_sql_dump, generate_erd_with_graphviz
# Parse schema dump
with open("schema.dump", "r", encoding='utf-8') as file:
sql_content = file.read()
# Extract database structure
tables, foreign_keys, triggers, errors = parse_sql_dump(sql_content)
# Generate interactive ERD
if not errors:
generate_erd_with_graphviz(
tables=tables,
foreign_keys=foreign_keys,
output_file="enterprise_diagram",
rankdir='TB',
packmode='graph'
)
print("โ
Enterprise ERD generated successfully!")
else:
print("โ ๏ธ Parsing errors:", errors)
โ๏ธ Complete Command-Line Reference
Core Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
input_file |
Required | - | Path to the PostgreSQL dump file |
-o, --output |
String | schema_erd |
Output file name (without extension) |
--view |
Flag | false |
Open the generated SVG in a browser |
--show-standalone |
String | true |
Show/hide tables with no foreign key relationships |
Layout & Positioning
| Argument | Type | Default | Options | Description |
|---|---|---|---|---|
--packmode |
String | array |
array, cluster, graph |
Graphviz packmode - Controls how components are packed together |
--rankdir |
String | TB |
TB, LR, BT, RL |
Graphviz rankdir - Graph direction (Top-Bottom, Left-Right, etc.) |
--esep |
String | 8 |
Any number | Graphviz esep - Edge separation distance in points |
--node-sep |
String | 0.5 |
Any number | Graphviz nodesep - Minimum distance between nodes |
--rank-sep |
String | 1.2 |
Any number | Graphviz ranksep - Distance between ranks/levels |
Typography & Styling
| Argument | Type | Default | Description |
|---|---|---|---|
--fontname |
String | Arial |
Font family for all text elements |
--fontsize |
Integer | 18 |
Font size for graph title/labels |
--node-fontsize |
Integer | 14 |
Font size for table names and column text |
--edge-fontsize |
Integer | 12 |
Font size for relationship labels |
--node-style |
String | rounded,filled |
Graphviz node style (e.g., filled, rounded,filled) |
--node-shape |
String | rect |
Graphviz node shape (e.g., rect, ellipse, box) |
Color & Visual Enhancement
| Argument | Type | Default | Description |
|---|---|---|---|
--saturate |
Float | 1.8 |
Color saturation multiplier for table backgrounds |
--brightness |
Float | 1.0 |
Brightness adjustment for table colors |
Understanding Graphviz Parameters
Packmode Options
array(default): Tables arranged in a regular grid patterncluster: Groups related tables together spatiallygraph: Optimizes overall graph layout, best for complex schemas
Rankdir Options
TB(Top-Bottom): Traditional vertical flow, tables flow downwardLR(Left-Right): Horizontal flow, good for wide displaysBT(Bottom-Top): Reverse vertical flowRL(Right-Left): Reverse horizontal flow
Distance Parameters
esep: Controls spacing between edges (relationship lines)node-sep: Minimum distance between table nodesrank-sep: Distance between different levels/ranks of tables
Advanced Filtering
Tables are automatically excluded based on common patterns:
- Views:
vw_*,*_view - Temporary:
*_temp,*_tmp,temp_* - Backup:
*_bk,*_backup,*_old - Audit/Log:
*_log,*_audit,audit_* - Duplicates:
*_dups,*_duplicates - Archives:
*_archive,archive_*
Use --show-standalone false to hide tables with no foreign key relationships.
๐ฏ Interactive Components
The generated SVG includes several interactive panels that can be moved, resized, and minimized:
๐ Metadata Panel
Displays comprehensive information about your database schema:
- Schema statistics (table count, columns, relationships)
- Generation parameters used
- File information and timestamps
- Interactive controls (minimize, close, drag to reposition)
๐บ๏ธ Miniature Overview
Navigate large schemas effortlessly:
- Interactive minimap with viewport indicator
- Click to jump to specific schema areas
- Drag viewport for precise navigation
- Resizable panel - make it larger for detailed navigation
๐ Selection Details
View and export detailed SQL information:
- Table definitions with column details and constraints
- Foreign key relationships with full SQL syntax
- Trigger information including execution details
- Copy button for instant clipboard access
- Download button for formatted text export
- Enterprise-friendly emoji-free output option
๐ Selected Tables
==================
public_franchises
public_association_map
public_ecommerce
...
๐ Foreign Key Relationships
============================
๐ franchise_id โ id
ALTER TABLE ONLY public.association_map
ADD CONSTRAINT association_map_franchise_id_fkey
FOREIGN KEY (franchise_id) REFERENCES public.franchises(id)
ON DELETE CASCADE;
For detailed testing instructions, see the Testing Guide.
Quality metrics:
- โ 95%+ code coverage
- โ 70+ comprehensive tests
- โ 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
๐จ Error Handling & Reliability
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
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
Welcome 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
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
Testing
The project includes both unit tests (Python) and functional tests (browser-based).
Quick Start
Use the provided test runner script which handles environment setup:
# Run unit tests
./run-tests.sh
# Run browser tests
./run-tests.sh --browser
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.2.53.tar.gz.
File metadata
- Download URL: pypgsvg-1.2.53.tar.gz
- Upload date:
- Size: 1.5 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
15d54ddeb0e023235f8ea6aebce0b5cfbc008fd7b71a067a1f4b9dba79218085
|
|
| MD5 |
8bc989572933d77483996f71b7b4d5b9
|
|
| BLAKE2b-256 |
f91625981a6af4c7b34f9c6e41d93a90df73756d0446bf34ce1b524e3ff13bfc
|
Provenance
The following attestation bundles were made for pypgsvg-1.2.53.tar.gz:
Publisher:
publish.yml on blackburnd/pypgsvg
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pypgsvg-1.2.53.tar.gz -
Subject digest:
15d54ddeb0e023235f8ea6aebce0b5cfbc008fd7b71a067a1f4b9dba79218085 - Sigstore transparency entry: 1109860385
- Sigstore integration time:
-
Permalink:
blackburnd/pypgsvg@d8d8328849bfe3323a5c0854106c44f9aa60baee -
Branch / Tag:
refs/tags/v1.2.53 - Owner: https://github.com/blackburnd
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d8d8328849bfe3323a5c0854106c44f9aa60baee -
Trigger Event:
push
-
Statement type:
File details
Details for the file pypgsvg-1.2.53-py3-none-any.whl.
File metadata
- Download URL: pypgsvg-1.2.53-py3-none-any.whl
- Upload date:
- Size: 64.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ed5f1295c3ef3f567299571d575825ec9e8ced34ecbc0953f4a1b3a69a4f60e
|
|
| MD5 |
26d82b387d954770e5b168db3db5bf3f
|
|
| BLAKE2b-256 |
aa6a33df8fc9cdea16666f95da4af1be31f8391765674c75e03696f8d2360f84
|
Provenance
The following attestation bundles were made for pypgsvg-1.2.53-py3-none-any.whl:
Publisher:
publish.yml on blackburnd/pypgsvg
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pypgsvg-1.2.53-py3-none-any.whl -
Subject digest:
1ed5f1295c3ef3f567299571d575825ec9e8ced34ecbc0953f4a1b3a69a4f60e - Sigstore transparency entry: 1109860401
- Sigstore integration time:
-
Permalink:
blackburnd/pypgsvg@d8d8328849bfe3323a5c0854106c44f9aa60baee -
Branch / Tag:
refs/tags/v1.2.53 - Owner: https://github.com/blackburnd
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d8d8328849bfe3323a5c0854106c44f9aa60baee -
Trigger Event:
push
-
Statement type: