Skip to main content

Database Connection

To obtain a database connection, use the Database interface:

let db: DatabaseConnection = Database::connect("protocol://username:password@host/database").await?;

protocol can be mssql:, etc.

host is usually localhost, a domain name or an IP address.


If you can't get localhost to work, try putting in an IP address and port number, e.g. or even 192.168.x.x.

Under the hood, a sqlz::Pool is created and owned by DatabaseConnection.

Each time you call execute or query_one/all on it, a connection will be acquired and released from the pool.

Multiple queries will execute in parallel as you await on them.

Connection String

Here are some tips for database specific options:


Specify a schema

If the schema is dbo, simply write:


Or, specify the schema name by providing an extra currentSchema query param.


Trust Peer Certificate

You can trust peer certificate by providing an extra trustCertificate query param.


Connect Options

To configure the connection, use the ConnectOptions interface:

let mut opt = ConnectOptions::new("protocol://username:password@host/database");

let db = Database::connect(opt).await?;

Checking Connection is Valid

Checks if a connection to the database is still valid.

|db: DatabaseConnection| {
assert!(matches!(, Err(DbErr::ConnectionAcquire)));

Closing Connection

The connection will be automatically closed on drop. To close the connection explicitly, call the close method.

let db = Database::connect(url).await?;

// Closing connection here