Skip to main content

Generate SVG Entity Relationship Diagrams from PostgreSQL database dumps

Project description

Python ERD Generator from Postgres Schema Dump File

pypgsvg is an open-source Python application that parses PostgreSQL schema SQL dump files and generates Directed Entity Relationship Diagrams (ERDs) using Graphviz. It includes features such as overview controls, edge highlighting, and introspection to display the SQL used to generate table nodes and edges, making it easier to debug PostgreSQL data structures.


In a past life I had been tasked with showing what the normalized postgresql database looks like for employers who do not want to pay for postgres tools that have the graphical tools. There are certainly usable free ones out there, and at the time required the installation of Java. So admittedly this started as an academic excersise. By no means is this even an alledgedly full throated tool, but takes most diagraph args.

Some versions of this saved me hours of time in explainations, now easier to share.

Features

  • Parse PostgreSQL dump files to extract table definitions and relationships.
  • Generate interactive SVG ERDs with automatic color coding.
  • Support for complex SQL structures, including foreign keys, constraints, and various data types.
  • Table filtering to exclude temporary or utility tables.
  • Accessible color palette with proper contrast for readability.
  • Comprehensive test suite with >80% code coverage.

Installation

  1. Install pypgsvg:

    pip install pypgsvg
    
  2. Ensure Graphviz is installed:

    • macOS: brew install graphviz
    • Ubuntu/Debian: sudo apt-get install graphviz
    • Windows: Download from Graphviz.org.

Usage

Command-Line Usage

Generate an ERD from your SQL dump file:

python -m src.pypgsvg Samples/schema.dump --output your_database_erd --rankdir TB --node-sep 4 --packmode graph

View the diagram immediately after generation:

python -m src.pypgsvg Samples/schema.dump --view

The following screenshots were generated from the dump file in the samples directory

Demo Images

Python API Usage

For programmatic use:

from src.pypgsvg import parse_sql_dump, generate_erd_with_graphviz

# Load SQL dump
with open("your_database_dump.sql", "r", encoding='utf-8') as file:
    sql_content = file.read()

# Parse tables and relationships
tables, foreign_keys, errors = parse_sql_dump(sql_content)

# Generate ERD
if not errors:
    generate_erd_with_graphviz(tables, foreign_keys, "database_diagram")
    print("ERD generated successfully!")
else:
    print("Parsing errors found:", errors)

Components

Metadata

Displays information about the generated SVG and the options used. Metadata

Overview

A minimap to quickly navigate larger SVG files. Overview

Selection SQL

Highlights SQL generation text for selected elements. Single-click inside the content area converts it to a text area for copy/paste. SQL


Testing

Run the complete test suite:

PYTHONPATH=src python -m pytest tests/tests

Run specific test categories:

pytest -m unit          # Unit tests only
pytest -m integration   # Integration tests only

Generate an HTML coverage report:

pytest --cov-report=html
open htmlcov/index.html

Project Structure

├── src/
│   └── create_graph.py          # Main application code
├── tests/
│   ├── conftest.py              # Test fixtures and configuration
│   ├── test_utils.py            # Tests for utility functions
│   ├── test_parser.py           # Tests for SQL parsing
│   ├── test_erd_generation.py   # Tests for ERD generation
│   └── test_integration.py      # Integration tests
├── requirements.txt             # Python dependencies
├── pyproject.toml               # pytest configuration
└── README.md                    # Documentation

Configuration

Table Exclusion

The application automatically excludes tables matching certain patterns:

  • Views (vw_)
  • Backup tables (bk)
  • Temporary fix tables (fix)
  • Duplicate tables (dups, duplicates)
  • Match tables (matches)
  • Version logs (versionlog)
  • Old tables (old)
  • Member data (memberdata)

Color Palette

The ERD uses an accessible color palette with automatic contrast calculation for text readability following WCAG guidelines.


Supported SQL Features

  • CREATE TABLE statements with various column types.
  • ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY.
  • Quoted identifiers and complex data types (e.g., numeric, timestamp, jsonb).
  • Multiple constraint variations.

Error Handling

The application includes comprehensive error handling for:

  • Malformed SQL syntax.
  • Missing table references in foreign keys.
  • Unicode encoding issues.
  • File reading errors.

Contributing

  1. Follow PEP 8 style guidelines.
  2. Write tests for new functionality.
  3. Maintain >90% test coverage.
  4. Use type hints where appropriate.
  5. Update documentation as needed.

Dependencies

  • graphviz>=0.20.1 - For generating diagrams.
  • pytest>=7.4.0 - Testing framework.
  • pytest-cov>=4.1.0 - Coverage reporting.
  • pytest-mock>=3.11.0 - Mocking utilities.

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

pypgsvg-1.0.74.tar.gz (160.6 kB view details)

Uploaded Source

Built Distribution

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

pypgsvg-1.0.74-py3-none-any.whl (20.0 kB view details)

Uploaded Python 3

File details

Details for the file pypgsvg-1.0.74.tar.gz.

File metadata

  • Download URL: pypgsvg-1.0.74.tar.gz
  • Upload date:
  • Size: 160.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for pypgsvg-1.0.74.tar.gz
Algorithm Hash digest
SHA256 75510a8e27aca6e7f09c45f1cb412f168ac97f11984af2d35de6cf46a3102beb
MD5 d34fb3fd676a8f25ad7589f1684ef2b2
BLAKE2b-256 99004aa0f0381366aef07ae5236734dc3879c410cd724c25d83eab9dcaf8153d

See more details on using hashes here.

File details

Details for the file pypgsvg-1.0.74-py3-none-any.whl.

File metadata

  • Download URL: pypgsvg-1.0.74-py3-none-any.whl
  • Upload date:
  • Size: 20.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for pypgsvg-1.0.74-py3-none-any.whl
Algorithm Hash digest
SHA256 bdd2066d844541ef0b241d75a0a0bbaada7e0ca39ce1629305fa678bbf6da571
MD5 58d755075e61e3b054b1cd3e2e426adc
BLAKE2b-256 0505aa8d70b4caf08796b11e1dd6c1230f19386a018c3e5c230d71413d3fe0fa

See more details on using hashes here.

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