Skip to content
Owais Khan Software Reviews

Postgres DDL to Mermaid Converter

Paste Postgres CREATE TABLE statements or pg_dump --schema-only output and get a Mermaid ER diagram with every key marked and every relationship’s cardinality worked out from your constraints — ready for a README or Mermaid Live.

Everything runs locally: your schema never leaves your browser.

Convert Postgres DDL to a Mermaid ER diagram

Open in Mermaid Live

4 tables, 3 relationships, 1 enum.

Reading the example

orders.user_id is nullable, so a user is zero or one on the orders side and the line is users |o..o{ orders. order_items.order_id is NOT NULL and part of the item’s primary key, which makes it an identifying relationship: orders ||--o{ order_items, drawn solid. profiles.user_id is the whole primary key, so each user has at most one profile: users ||--o| profiles. The CHECK constraint is dropped, the enum’s values move into the column comment, and timestamp with time zone becomes timestamptz so Mermaid can parse it.

CREATE TABLE to Mermaid ER diagram, without guessing

Most create table to mermaid er diagram scripts draw every foreign key as one-to-many and stop there. The constraints in your DDL say more than that — whether a parent is optional, whether the child is one-to-one, whether the key is identifying — and this reads all of it. It is also a general SQL to Mermaid ERD converter for plain CREATE TABLE syntax, but Postgres-specific pieces such as enums, identity columns, COMMENT ON and ALTER TABLE ONLY are what it is built around.

Postgres schema to Mermaid from a live database

To turn an existing Postgres schema to Mermaid, dump the structure and paste the result: pg_dump --schema-only --no-owner mydb > schema.sql. Functions, triggers, indexes, grants and sequences in the dump are ignored; tables, keys, enums and column comments are read.

Where these rules come from

The relationship notation, the attribute-type rules and the PK/FK/UK markers are from Mermaid’s entity relationship diagram documentation, and the output was checked against Mermaid 11’s own parser. The type aliases are the ones listed in PostgreSQL’s data type documentation.

Frequently asked questions

How does the converter decide one-to-one versus one-to-many?
From the foreign key column itself. If the column is NOT NULL, every child row must have a parent, so the parent end is "exactly one" (||); if it is nullable, it is "zero or one" (|o). If the column is UNIQUE, or is the whole primary key, a parent can have at most one child, so the relationship is one-to-one (o|); otherwise it is one-to-many (o{). A foreign key that is part of the child’s primary key — like order_items.order_id above — is an identifying relationship and is drawn as a solid line; the rest are dashed.
Does it handle composite primary keys and foreign keys declared with ALTER TABLE?
Yes. Table-level PRIMARY KEY (a, b), FOREIGN KEY (a, b) REFERENCES ... and UNIQUE constraints are read, named or not, and so are ALTER TABLE ... ADD CONSTRAINT statements — which is how pg_dump --schema-only writes every key. A composite foreign key is mandatory only if all of its columns are NOT NULL. A foreign key that points at a table you did not paste is listed under the output rather than drawn.
Why does timestamp with time zone become timestamptz?
Mermaid only accepts an attribute type that is a single word, so a multi-word Postgres type breaks the diagram with a parse error. The converter uses Postgres’s own short aliases instead — timestamptz, varchar, float8, varbit — which mean exactly the same thing. A type with two parameters, such as numeric(10,2), is written as numeric with the full type kept in the attribute comment, so no information is lost.
How are ENUM and JSONB columns shown?
A column whose type is an enum created in the same DDL with CREATE TYPE ... AS ENUM keeps the enum’s name as its type, and the allowed values are listed in the attribute comment. JSONB, UUID, arrays and other single-word types pass through unchanged; an array such as text[] stays an array.
Can I export the diagram as SVG or PNG?
Open the result in Mermaid Live with the button above; its Actions panel downloads SVG and PNG. Or paste the Markdown block into a README, issue or wiki page on GitHub or GitLab, which render Mermaid diagrams natively, and into Notion or Obsidian, which do the same.
Is my schema uploaded anywhere?
No. The conversion runs entirely as a small script inside this page; your DDL never leaves your browser, and there is no upload, no API call and no analytics here. The Mermaid Live button carries the diagram in the link itself, and only leaves the page when you click it. That privacy is enforced rather than promised: this site’s test suite scans the shipped HTML for every browser API capable of sending data off the page and fails the build if it finds one.