Skip to content

db-diagram

test PyPI version

This package provides a CLI and library for generating Mermaid Entity Relationship Diagrams based on an existing database connection URL and/or SQLAlchemy metadata.

erDiagram ab_group_role { INTEGER id PK INTEGER group_id FK INTEGER role_id FK } ab_group_role }o--|| ab_group : "group_id:id" ab_group_role }o--|| ab_role : "role_id:id" ab_group { INTEGER id PK VARCHAR name VARCHAR label VARCHAR description } ab_user_group { INTEGER id PK INTEGER user_id FK INTEGER group_id FK } ab_user_group }o--|| ab_group : "group_id:id" ab_role { INTEGER id PK VARCHAR name } ab_permission_view_role { INTEGER id PK INTEGER permission_view_id FK INTEGER role_id FK } ab_permission_view_role }o--|| ab_role : "role_id:id" ab_user_role { INTEGER id PK INTEGER user_id FK INTEGER role_id FK } ab_user_role }o--|| ab_role : "role_id:id"

Installation

You can install db-diagram with pip:

pip3 install db-diagram

You will also need to have libraries installed for the SQLAlchemy dialect corresponding to your database, unless you are using MySQL/MariaDB or SQLite. For convenience, installation extras are defined for a handful of dialects:

  • pip3 install 'db-diagram[postgresql]'
  • pip3 install 'db-diagram[snowflake]'
  • pip3 install 'db-diagram[databricks]'

Usage

You can utilize db-diagram as a CLI (from a shell or command prompt), or as a python library (from db_diagram import write_markdown). The CLI will be more convenient under most circumstances, however if you want to generate diagrams from SQLAlchemy metadata rather than a connection URL, such as you might for a SQLAlchemy ORM, you will want to use db_diagram as a python library. Pease refer to these examples for reference concerning output.