Inspecting existing schemas with Atlas
Automatic Schema Inspection
Many projects begin with an existing database that users wish to start managing with Atlas. In this case, instead of having developers learn the Atlas Language and reverse engineer a schema definition file that precisely describes the existing database, Atlas supports automatic schema inspection.
With automatic schema inspection, users simply provide Atlas with a connection string to their target database and Atlas prints out a schema definition file in the Atlas language that they can use as the starting point for working with this database.
By default, running atlas schema inspect
inspects only schemas, tables, and their associated indexes and
constraints such as foreign keys and checks.
Views, materialized views, functions, procedures, triggers, sequences, domains, extensions, and additional database features are available to Atlas Pro users. To include these resources in the inspection, use the following command:
atlas login
Flags
When using schema inspect
to inspect an existing database, users may supply multiple
parameters:
--url
(-u
accepted as well) - the URL of database to be inspected.--schema
(optional, may be supplied multiple times) - schemas to inspect within the target database.--exclude
(optional) - filter out resources matching the given glob pattern.--format
(optional) - Go template to use to format the output.--web
(-w
accepted as well) - visualize the schema as an ERD on Atlas Cloud. See an example here.
Examples
Inspect a database
The following commands demonstrate how to inspect the entire database, including all its schemas:
- MySQL
- MariaDB
- PostgreSQL
- SQLite
- SQL Server
- ClickHouse
- Redshift
atlas schema inspect -u "mysql://localhost"
atlas schema inspect -u "mysql://user:pass@localhost:3306"
atlas schema inspect -u "maria://localhost"
atlas schema inspect -u "maria://user:pass@localhost:3306"
atlas schema inspect -u "postgres://localhost:5432/database"
atlas schema inspect -u "postgres://postgres:pass@localhost:5432/database?sslmode=disable"
atlas schema inspect -u "sqlite://file.db"
atlas schema inspect -u "sqlite://file?cache=shared&mode=memory"
atlas schema inspect -u "sqlserver://localhost:1433?database=master&mode=database"
atlas schema inspect -u "sqlserver://user:pass@localhost:1433?database=master&mode=database"
atlas schema inspect -u "clickhouse://localhost:9000"
atlas schema inspect -u "clickhouse://user:pass@localhost:9000?secure=true"
atlas schema inspect -u "redshift://redshift-cluster:5439/database"
atlas schema inspect -u "redshift://user:pass@redshift-cluster:5439/database?sslmode=disable"
Inspect a schema
The following commands show how to inspect a single schema:
- MySQL
- MariaDB
- PostgreSQL
- SQL Server
- ClickHouse
- Redshift
atlas schema inspect -u "mysql://localhost/schema"
atlas schema inspect -u "mysql://user:pass@localhost:3306/schema"
atlas schema inspect -u "maria://localhost/schema"
atlas schema inspect -u "maria://user:pass@localhost:3306/schema"
atlas schema inspect -u "postgres://localhost:5432/database?search_path=public"
atlas schema inspect -u "postgres://postgres:pass@localhost:5432/database?search_path=public&sslmode=disable"
atlas schema inspect -u "sqlserver://localhost:1433?database=master"
atlas schema inspect -u "sqlserver://user:pass@localhost:1433?database=master"
atlas schema inspect -u "clickhouse://localhost:9000/database"
atlas schema inspect -u "clickhouse://user:pass@localhost:9000/database?secure=true"
atlas schema inspect -u "redshift://redshift-cluster:5439/database?search_path=public"
atlas schema inspect -u "redshift://user:pass@redshift-cluster:5439/database?search_path=public&sslmode=disable"
Inspect multiple schemas
The following commands show how to inspect multiple schemas:
- MySQL
- MariaDB
- PostgreSQL
- SQL Server
- ClickHouse
- Redshift
atlas schema inspect -u "mysql://localhost" --schema schema1 --schema schema2
atlas schema inspect -u "mysql://user:pass@localhost:3306" -s schema1,schema2
atlas schema inspect -u "maria://localhost" --schema schema1 --schema schema2
atlas schema inspect -u "maria://user:pass@localhost:3306" -s schema1,schema2
atlas schema inspect -u "postgres://localhost:5432/database?sslmode=disable" --schema schema1 --schema schema2
atlas schema inspect -u "postgres://postgres:pass@localhost:5432/database?sslmode=disable" -s schema1,schema2
atlas schema inspect -u "sqlserver://localhost:1433?database=master&mode=database" --schema schema1 --schema schema2
atlas schema inspect -u "sqlserver://user:pass@localhost:1433?database=master&mode=database" -s schema1,schema2
atlas schema inspect -u "clickhouse://localhost:9000/database" --schema schema1 --schema schema2
atlas schema inspect -u "clickhouse://user:pass@localhost:9000/database?secure=true" -s schema1,schema2
atlas schema inspect -u "redshift://user:pass@redshift-cluster:5439/database" --schema schema1 --schema schema2
atlas schema inspect -u "redshift://user:pass@redshift-cluster:5439/database?search_path=public&sslmode=disable" -s schema1,schema2
Exclude Schemas
The following commands show how to exclude schemas that match a glob pattern from the inspection:
atlas schema inspect -u "mysql://localhost" --exclude "internal"
atlas schema inspect -u "mysql://localhost" --exclude "schema_*"
Exclude Schema Resources
The following commands show how to exclude schema resources (objects) that match a glob pattern from the inspection:
- Database Scope
- Schema Scope
When inspecting a database (multiple schemas), the first glob pattern matches the schema name, and the second matches the object name:
# Exclude all policies and functions from the public schema.
atlas schema inspect -u "postgres://localhost:5432/database" --exclude 'public.*[type=policy|function]'
# Exclude all policies and functions that match the pattern from all schemas.
atlas schema inspect -u "postgres://localhost:5432/database" --exclude '*.*[type=policy|function]'
When inspecting a specific schema, the first glob pattern matches the object name:
atlas schema inspect -u "postgres://localhost:5432/database" --exclude '*[type=policy|function]'
atlas schema inspect -u "postgres://localhost:5432/database" --exclude '*[type=policy|function]'
Exclude Tables
The following commands show how to exclude tables that match a glob pattern from the inspection:
- Database Scope
- Schema Scope
When inspecting a database (multiple schemas), the first glob pattern matches the schema name, and the second matches the table name:
atlas schema inspect -u "mysql://localhost" --exclude "*.prefix_*"
atlas schema inspect -u "mysql://localhost" --exclude "schema.table"
atlas schema inspect -u "mysql://localhost" --exclude "schema.t*[type=table]" --exclude "schema.e*[type=enum]"
When inspecting a specific schema, the first glob pattern matches the table name:
atlas schema inspect -u "mysql://localhost" --exclude "prefix_*"
atlas schema inspect -u "mysql://localhost" --exclude "table"
atlas schema inspect -u "mysql://localhost" --exclude "t*[type=table]" --exclude "e*[type=enum]"
Exclude Table Resources
The following commands show how to exclude columns, indexes or foreign-keys that match a glob pattern from the inspection:
- Database Scope
- Schema Scope
When inspecting a database (multiple schemas), the first glob pattern matches the schema name, and the second matches the table name:
atlas schema inspect -u "mysql://localhost" --exclude "*.*.prefix_*"
atlas schema inspect -u "mysql://localhost" --exclude "public.*.c1"
atlas schema inspect -u "mysql://localhost" --exclude "public.*.c*[type=column|index]"
When inspecting a specific schema, the first glob pattern matches the table name:
atlas schema inspect -u "mysql://localhost" --exclude "*.prefix_*"
atlas schema inspect -u "mysql://localhost" --exclude "*.c1"
atlas schema inspect -u "mysql://localhost" --exclude "*.c*[type=column|index]"
SQL Format
By default, the output of schema inspect
is in the Atlas DDL. However, you can use SQL to describe the desired schema
in all commands that are supported by Atlas DDL. To output the schema in SQL format, use the --format
flag as follows:
atlas schema inspect -u "mysql://localhost" --format "{{ sql . }}"
JSON Format
Atlas can output a JSON document that represents the database schema. This representation allows users to use tools
like jq
to analyze the schema programmatically.
atlas schema inspect -u '<url>' --format '{{ json . }}'
Visualize schemas
Atlas can generate an Entity Relationship Diagram (ERD) for the inspected schemas. The following command shows how to generate an ERD for inspected schemas:
atlas schema inspect -u '<url>' -w