LangStop · PostgreSQL · ERD Generator

PostgreSQL ERD Generator — Visualize Postgres Schemas

Import SQL with the PostgreSQL option to turn CREATE statements from a migration file or a schema export into an entity-relationship diagram. Tables, columns, keys, enums and foreign keys come out as DBML, which the canvas draws as tables and relationships.

The conversion runs in your browser. The PostgreSQL grammar is part of @dbml/core, which the app loads when you click Apply Schema, and no request leaves the page.

How this tool handles it

The PostgreSQL option uses @dbml/core's PostgreSQL grammar. That grammar is not a full PostgreSQL parser: it reads the statements below and skips the rest.

  • Read: CREATE TABLE with column types, NOT NULL, UNIQUE, DEFAULT, PRIMARY KEY and identity columns; CREATE TYPE ... AS ENUM; CREATE INDEX and CREATE UNIQUE INDEX; ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY with ON DELETE and ON UPDATE; COMMENT ON TABLE.
  • Skipped: CREATE VIEW, CREATE SCHEMA (the schema name still appears on the tables inside it), and any statement the grammar does not model.
  • Schema names: a table in the public schema is written without its prefix. A table in any other schema keeps it.
  • Types: the type name is copied as written. text[] becomes a quoted type name, and numeric(10,2) keeps its precision.

Worked example

This schema has a named schema, an enum type, an identity column, an array, a composite key, a unique constraint, a foreign key added with ALTER TABLE, a partial unique index and a view:

CREATE SCHEMA billing;
CREATE TYPE mood AS ENUM ('happy', 'sad');
CREATE TABLE billing.invoices (
  invoice_id integer GENERATED ALWAYS AS IDENTITY,
  account_id integer NOT NULL,
  tags text[],
  payload jsonb,
  mood mood,
  line_no integer NOT NULL,
  PRIMARY KEY (invoice_id, line_no),
  UNIQUE (account_id, line_no)
);
CREATE TABLE billing.accounts (
  id integer PRIMARY KEY,
  name varchar(80) DEFAULT 'x'
);
ALTER TABLE billing.invoices ADD CONSTRAINT invoices_account_fk FOREIGN KEY (account_id) REFERENCES billing.accounts (id) ON DELETE SET NULL;
CREATE UNIQUE INDEX invoices_tag_uq ON billing.invoices (account_id) WHERE line_no > 0;
CREATE VIEW billing.v AS SELECT 1;
COMMENT ON TABLE billing.accounts IS 'accounts';

The DBML that comes out:

Enum "mood" {
  "happy"
  "sad"
}
 
Ref "invoices_account_fk":"billing"."accounts"."id" < "billing"."invoices"."account_id" [delete: set null]
 
Table "billing"."invoices" {
  "invoice_id" integer [increment]
  "account_id" integer [not null]
  "tags" "text[]"
  "payload" jsonb
  "mood" mood
  "line_no" integer [not null]
 
  Indexes {
    (invoice_id, line_no) [pk]
    (account_id, line_no) [unique]
    account_id [unique, name: "invoices_tag_uq"]
  }
}
 
Table "billing"."accounts" {
  "id" integer [pk]
  "name" varchar(80) [default: 'x']
  Note: 'accounts'
}

The CREATE VIEW is not in the output. The partial index is in the output, but its WHERE line_no > 0 is gone (see gotcha 2).

Gotchas

1. A CHECK constraint makes the DBML invalid. A column check such as plan text NOT NULL CHECK (plan IN ('free', 'pro')) is written as a check: setting in the column's brackets. A table-level ALTER TABLE ... CHECK is written as a Checks block. The DBML parser rejects both, and it reports the column setting on line 5 of the output. Remove the CHECK clauses before you import.

2. A partial unique index becomes a plain unique index. Input:

CREATE UNIQUE INDEX invoices_tag_uq ON billing.invoices (account_id) WHERE line_no > 0;

Output:

account_id [unique, name: "invoices_tag_uq"]

The WHERE clause is dropped, so the diagram shows account_id as unique for every row. Do not read the diagram as the constraint your database enforces.

3. The public prefix disappears. CREATE TABLE public.customers appears as Table "customers". Tables in other schemas keep the prefix, as "billing"."invoices" shows above. Check the table name in the diagram against your schema before you rely on it.

4. Views are not drawn. The importer skips CREATE VIEW, so a view does not appear as a table. Only the statement types listed above reach the output.

Frequently asked questions

Are schemas other than public kept?

Yes. A table in a named schema keeps the schema in the output, as "billing"."invoices". Tables in the public schema are written without the prefix, so "public"."customers" becomes "customers".

Do enums, identity columns and arrays import?

Yes, in the sample on this page. CREATE TYPE ... AS ENUM becomes an Enum block, GENERATED ALWAYS AS IDENTITY and bigserial become increment, and an array type such as text[] is written as a quoted type name.

Why does my PostgreSQL import fail with a CHECK constraint?

The generated DBML contains a check setting or a Checks block that the DBML parser does not accept. Remove the CHECK clauses from the SQL before you import, or keep the constraint in your migration files and only diagram the structure.

Which statements does the PostgreSQL import read?

CREATE TABLE, CREATE TYPE ... AS ENUM, CREATE INDEX, ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY, and COMMENT ON TABLE, which becomes a Note. CREATE VIEW statements are not in the output.