Generate DBML from SQL Database
Overview
dbdocs & dbml offer multiple ways for you to generate DBML code from your database, via db2dbml
and sql2dbml
commands.
db2dbml: Generate DBML from Direct DB Connection
This method connects directly to your database and reads off your database schema (usually from INFORMATION_SCHEMA) to generate DBML code.
Make sure you have dbdocs CLI installed. Then run:
$ dbdocs db2dbml postgres <connection-string> -o database.dbml
✔ Connecting to database <db-name>... done.
✔ Generating DBML... done.
✔ Wrote to database.dbml
Is it safe to allow dbdocs CLI to connect to my database? Yes
- Everything runs on your local machine, and the database credentials are not tracked or sent anywhere.
- The database connection is secured.
- The code is open source from the @dbml/cli & @dbml/connector packages and can be checked here.
Supported databases: PostgreSQL, MySQL, MSSQL, Snowflake, BigQuery.
sql2dbml: Generate DBML from SQL DDL (sql dump)
Alternatively, if you don’t have direct database connection access (or prefer not to do it), you can export a SQL DDL dump file and use the sql2dbml
command instead. Learn more about it here.