URLs
Atlas uses a standard URL format to connect to databases and load schemas and migrations from various sources. The format below covers the supported parts of a URL, with subsequent sections providing more detailed examples.
driver://[username[:password]@]address/[schema|database][?param1=value1&...¶mN=valueN]
To inspect a database using a URL, refer to one of the examples below:
- MySQL
- MariaDB
- PostgreSQL
- SQLServer
- SQLite
- ClickHouse
- Redshift
- Docker
Connecting to a local MySQL server (all schemas/databases):
mysql://localhost:3306/
Connecting to a specific MySQL schema (database) with a username and password:
mysql://user:pass@localhost:3306/schema
Connecting using Unix Sockets:
mysql+unix:///tmp/mysql.sock
mysql+unix://user:pass@/tmp/mysql.sock
mysql+unix://user@/tmp/mysql.sock?database=dbname
Connecting to a local MariaDB server (all schemas/databases):
maria://localhost:3306/
Connecting to a specific MariaDB schema (database) with a username and password:
maria://user:pass@localhost:3306/schema
Connecting using Unix Sockets:
maria+unix:///tmp/mysql.sock
maria+unix://user:pass@/tmp/mysql.sock
maria+unix://user@/tmp/mysql.sock?database=dbname
Connecting to a local PostgreSQL database named database
(all schemas):
postgres://localhost:5432/database
Connecting to a specific PostgreSQL schema named public
:
postgres://localhost:5432/database?search_path=public
Connecting to a local PostgreSQL with credentials and SSL disabled:
postgres://postgres:pass@0.0.0.0:5432/database?search_path=public&sslmode=disable
Connecting to a default schema of current user:
sqlserver://sa:P@ssw0rd0995@localhost:1433?database=master&mode=schema
Connecting to a local SQLServer database named master
(all schemas). The user need to have db_owner
role:
sqlserver://sa:P@ssw0rd0995@localhost:1433?database=master&mode=database
Azure Active Directory (AAD) authentication:
Use the fedauth
parameter to specify the AAD authentication method. For more information, see the document on the underlying driver.
azuresql://<instance>.database.windows.net?fedauth=ActiveDirectoryDefault&database=master
- The
mode
parameter is Atlas-specific and isn't used for opening the underlying connection. - The default
mode
isschema
. - The
azuresql
schema is used for AAD authentication with Azure SQL Database and Azure SQL Managed Instance.
Connecting to a local SQLite database (file):
sqlite://file.db
Connecting to an in-memory SQLite database (ephemeral). Useful for --dev-url
:
sqlite://file?mode=memory&_fk=1
Atlas also supports WebSocket connections to remote libsql
databases:
libsql+ws://database-url # For local environments
libsql+wss://database-url
Connecting to a local ClickHouse server (all schemas/databases):
clickhouse://localhost:9000
Connecting to a specific ClickHouse schema (database) with a username and password:
clickhouse://user:pass@localhost:9000/schema
Connecting to a specific ClickHouse schema with SSL enabled:
clickhouse://user:pass@localhost:9000/schema?secure=true
To connect ClickHouse Cloud,
we need to use native protocol port 9440
with SSL enabled:
clickhouse://user:pass@CLICKHOUSE-CLOUD-HOST:9440/schema?secure=true
Connecting to a specific Redshift cluster with a schema named public
:
redshift://user:pass@redshift-cluster:5439/database?search_path=public
Connecting to a specific Redshift cluster with a schema named public
with SSL disabled:
redshift://user:pass@redshift-cluster:5439/database?search_path=public&sslmode=disable
If you want to connect Redshift though Data API you can use the following URL:
AWS credentials are required to connect to Redshift via Data API. In this protocol, atlas doesn't support changing the schema on URL, the schema is based on default schema of the user. If you want to bind the connection to a specific schema, you can use the following SQL command:
ALTER USER [username] SET search_path = [schema];
Connecting to Serverless via IAM Identity:
redshift+http://workgroup([workgroup-name])/[database]
Connecting to Serverless via Secret ARN:
redshift+http://[arn]@workgroup([workgroup-name])/[database]
Connecting to provisioned Redshift cluster via IAM Identity:
redshift+http://cluster([cluster-name])/[database]
Connecting to provisioned Redshift cluster with database username
redshift+http://[dbuser]@cluster([cluster-name])/[database]
Connecting to provided Redshift cluster via Secret ARN:
redshift+http://[arn]@cluster([cluster-name])/[database]
- The default
mode
isschema
. - To change the connection to realm mode, use
mode=database
. - Use
timeout=5m
to set the timeout for the http client. Default is 5 minutes. - Use
polling=50ms
to set the polling interval when fetching the query results. Default is 50ms.
Atlas can spin up an ephemeral local docker container for you by specifying a special URL like below. This can be useful
if you need a dev database for schema validation or diffing. However, some images like mysql
/
mariadb
take quite some time to "boot", before they are ready to be used. For a smoother developing experience
consider spinning up a longer lived container by yourself.
# PostgreSQL database scope (all schemas).
docker://postgres/15/test
# PostgreSQL specific schema scope.
docker://postgres/15/test?search_path=public
# MySQL server scope (all schemas).
docker://mysql/8
# MySQL specific schema scope.
docker://mysql/8/test
# MySQL server scope (all schemas).
docker://maria/latest
# MySQL specific schema scope.
docker://maria/latest/test
When the database URL is set to a specific schema (e.g., mysql://:3306/dev
), the scope of the work done by Atlas
(inspection, diffing, planning, applying, etc.) is limited to one schema. As a result, DDL statements printed during
diffing or planning will be formatted without schema qualifiers and can be executed on any schema. e.g., table
instead
of schema.table
However, if the database URL does not specify a schema (e.g., mysql://:3306/
), Atlas operates on the selected schemas
(defaulting to all), and the generated DDL statements include schema qualifiers. e.g., schema.table
instead of table
.
Supported Schemes
Besides the standard database URLs mentioned above, Atlas supports various schemes for loading schemas and migration states:
file
The file://
scheme is used to load schema state from a local file or a directory. The supported extensions
are .sql
and .hcl
. For example:
file://path/to/schema.hcl
file://path/to/schema.sql
file://path/to/schemadir
file://path/to/migrations
file://path/to/migrations?version=20231201182011
atlas
The atlas://
scheme is used to load the state of a remote schema or a migrations directory from the Atlas Cloud, the
schema registry, and migrations artifactory of Atlas. For example:
atlas://dir-slug
atlas://dir-slug?version=20231201182011
atlas://dir-slug?tag=39e7e4e35fce7409bd26d25d8140061695d4ffd5
env
The env://
scheme is useful for referencing the state of a schema after it has been loaded by a data source. For example:
data "external_schema" "orm" {
program = [
...
]
}
env "dev" {
orm = data.external_schema.orm
}
atlas schema inspect --env dev -u env://orm
ent
The ent://
scheme is used to load the state an ent schema. For example:
ent://path/to/ent/schema
SSL/TLS Mode
The default SSL mode for Postgres is required
. Please follow the
Postgres documentation
for configuring your SSL connection for your database, or set SSL mode to disable
with the search parameter ?sslmode=disable
. For local databases,
disabling SSL is appropriate when inspecting and applying schema changes.
MySQL does not require TLS by default. However, you can require TLS
with the ?tls=true
search parameter.
Non-alphanumeric characters
Database URLs often contain passwords and other information which may contain non-alphanumeric characters.
These characters must be escaped using standard URL encoding, in order to be parsed correctly.
As a convenience, users may use the urlescape
function in an atlas.hcl
project file to escape
these characters automatically.
Suppose your password is h:e!:l:l:o
and it is stored as an environment variable named DB_PASSWORD
, you
can read this value and escape it using the urlescape
function:
locals {
db_pass = urlescape(getenv("DB_PASSWORD"))
}
env "local" {
url = "postgres://user:${local.db_pass}@localhost:5432/database"
}
The urlescape
function return the escaped value: h%3Ae%21%3Al%3Al%3Ao
.