My diagram only knows the pasted DDL

Short answerConnect the diagram tool to the database instead of pasting DDL. DbSchema Pro keeps the design in a model file in Git and syncs it with the live database, and the free DbSchema Community Edition reverse-engineers SQL and NoSQL databases. Free tools such as tbls, SchemaSpy and the DBML CLI also read the live schema. The free basics: dump the schema from the database and check the foreign keys.

01

Keep the design in DbSchema Pro and sync it with the live database

DbSchema Pro saves the design to a model file with the extension .dbs that works offline and lives in Git next to your code. Schema Synchronization compares that model with the live database, lists every difference and lets you choose for each one whether the model or the database wins; DbSchema then shows the CREATE and ALTER statements before they run. Pro also publishes HTML5, PDF and Markdown documentation, and you can buy it as a perpetual licence or monthly.

Good if the diagram has to match what is deployed, not what someone pasted last month

Editions and pricing · Git Collaboration · Schema Synchronization · Schema Documentation

Tool
Cost€€
EffortMedium
HelpsThis week
02

Connect DbSchema Community to the database instead of pasting DDL

DbSchema Community Edition is free for any use, needs no account and connects to more than 100 SQL and NoSQL databases, MongoDB included. In Reverse Engineer the Database, tick the schemas and press Import, and DbSchema draws the current tables and their relationships. Nothing is uploaded to a cloud. Saving the design to a file and syncing it are in DbSchema Pro.

Good if you want a correct picture of the real schema today

Editions and pricing · Connect to a Database · Features · Supported databases

Tool
CostFree
EffortLow
HelpsToday
03

Generate the diagram from the database with tbls in CI

tbls is a free command-line tool that connects to the database, writes Markdown documentation and draws ER diagrams. Run tbls doc in CI and commit the result. tbls diff shows the difference between the database schema and the generated document, so the build notices when they drift apart.

Good if your team already runs CI with access to a test database

Skip if CI cannot reach any database

GitHub

Tool
CostFree
EffortMedium
HelpsThis week
04

Generate your DBML from the database instead of writing it

If your diagram is kept as DBML, install the free DBML CLI with npm and run db2dbml with a connection string. It reads the live database, PostgreSQL, MySQL, SQL Server, Snowflake, BigQuery or Oracle, and writes the DBML file. Run it after every migration and commit the result, so the diagram starts from the real schema.

Good if your team likes diagrams as code and wants to keep DBML

Skip if you use a NoSQL database

DBML

Tool
CostFree
EffortLow
HelpsThis week
05

Publish HTML docs with diagrams using SchemaSpy

SchemaSpy is a free Java tool that reads an existing database and generates HTML documentation with entity relationship diagrams. It is a single .jar file, so you can run it after each release and share the HTML with everyone who needs the schema.

Good if people outside the dev team need to browse the schema

SchemaSpy

Tool
CostFree
EffortMedium
HelpsThis week
06

Export the schema from the database, not from memory

If you stay with pasted DDL, take it from the database every time. On PostgreSQL, pg_dump --schema-only dumps only the object definitions, without data. Commit the file next to your migrations, so the diff shows what changed before you paste it into the diagram tool again.

Good if you only need an up-to-date picture once in a while

Skip if the schema changes several times a week

PostgreSQL

Do it yourself
CostFree
EffortLow
HelpsToday
07

Check the diagram against the real foreign keys

Query the information_schema view referential_constraints, which lists every foreign key constraint in the current database. Compare the list with the relationships in your diagram.

Good if you are not sure how far the diagram has drifted

PostgreSQL

Do it yourself
CostFree
EffortLow
HelpsToday

When it's bigger than a quick fix

Bring in a DBA or a data architect when the diagram, the migration files and the production database all disagree and nobody can say which one is right, or when changes reach production outside your migrations.

PostgreSQL

Questions people ask

How do I generate an ER diagram from an existing database?

Use a tool that connects to the database and reads the schema, such as tbls or SchemaSpy, instead of drawing it by hand. Both write documentation with ER diagrams from the live database.

Can I generate DBML from a database?

Yes. The free DBML CLI has a db2dbml command that reads PostgreSQL, MySQL, SQL Server, Snowflake, BigQuery or Oracle and writes a DBML file.

How do I export only the schema from PostgreSQL?

Run pg_dump with --schema-only. It dumps the object definitions without data or statistics.

How can I tell whether my diagram is out of date?

Compare its relationships with the information_schema view referential_constraints, which lists every foreign key constraint in the database. tbls diff does a similar check against generated docs.

Sources

  1. DbSchema: Editions and pricing
  2. DbSchema: Git Collaboration
  3. DbSchema: Schema Synchronization
  4. DbSchema: Schema Documentation
  5. DbSchema: Connect to a Database
  6. DbSchema: Features
  7. DbSchema: Supported databases
  8. GitHub: k1LoW/tbls
  9. DBML: CLI
  10. SchemaSpy: Database Documentation Built Easy
  11. PostgreSQL: pg_dump
  12. PostgreSQL: referential_constraints
  13. PostgreSQL: Professional Services