Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/launchbadge/sqlx/llms.txt

Use this file to discover all available pages before exploring further.

SQLx supports connections to PostgreSQL, MySQL/MariaDB, and SQLite. You can open a single connection directly for simple use cases, or use a connection pool for applications that handle concurrent workloads. Both approaches use the same Connection trait and share the same URL format conventions.

Connection URL formats

Each database driver parses a connection URL in a driver-specific format.
postgres://[user[:password]@][host][:port]/[database][?param=value]
postgres://alice:secret@localhost:5432/mydb
postgres://localhost/mydb?sslmode=require
postgresql://localhost/mydb
Both postgres:// and postgresql:// are accepted scheme prefixes for PostgreSQL.

Single connections

Use Connection::connect() to open a single database connection directly. Import the Connection trait to bring the method into scope.
use sqlx::Connection;

// PostgreSQL
let mut conn = sqlx::PgConnection::connect("postgres://alice:secret@localhost/mydb").await?;

// MySQL
let mut conn = sqlx::MySqlConnection::connect("mysql://alice:secret@localhost/mydb").await?;

// SQLite (in-memory)
let mut conn = sqlx::SqliteConnection::connect("sqlite::memory:").await?;
A single connection executes all queries sequentially on that one connection object. Because Connection is not Clone, you must pass a mutable reference (&mut conn) to query finalizers.
use sqlx::{Connection, Row};

let mut conn = sqlx::PgConnection::connect("postgres://localhost/mydb").await?;

let row = sqlx::query("SELECT 1 + 1 AS result")
    .fetch_one(&mut conn)
    .await?;

let result: i32 = row.get("result");

The ConnectOptions trait

The ConnectOptions trait provides a typed, builder-style API for configuring a connection. Each database exposes its own implementation with driver-specific options.
DatabaseType
PostgreSQLPgConnectOptions
MySQLMySqlConnectOptions
SQLiteSqliteConnectOptions
ConnectOptions values can be constructed by parsing a URL string or built programmatically:
use sqlx::postgres::{PgConnectOptions, PgSslMode};

// Parse from a URL
let opts: PgConnectOptions = "postgres://localhost/mydb".parse()?;

// Build programmatically
let opts = PgConnectOptions::new()
    .host("localhost")
    .port(5432)
    .database("mydb")
    .username("alice")
    .password("secret")
    .ssl_mode(PgSslMode::Require);

let mut conn = sqlx::PgConnection::connect_with(&opts).await?;
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode};
use std::str::FromStr;

let opts = SqliteConnectOptions::from_str("sqlite:data.db")?
    .journal_mode(SqliteJournalMode::Wal)
    .create_if_missing(true);

let mut conn = sqlx::SqliteConnection::connect_with(&opts).await?;
Use ConnectOptions over raw URL strings when you need to configure options that have no URL representation, such as SQLite journal mode or per-connection statement logging levels.

Disabling statement logging

ConnectOptions exposes log_statements and disable_statement_logging methods for controlling how executed SQL is logged. This is useful in tests or when queries contain sensitive data:
use sqlx::ConnectOptions;
use sqlx::postgres::PgConnectOptions;

let opts = PgConnectOptions::new()
    .host("localhost")
    .database("mydb")
    .disable_statement_logging();

let mut conn = sqlx::PgConnection::connect_with(&opts).await?;

Single connection vs. connection pool

Single connection

Best for scripts, CLI tools, tests, or anywhere you need direct control over a single sequential flow of queries. Not Clone; must be passed as &mut conn.

Connection pool

Best for web servers and long-lived applications where multiple tasks issue queries concurrently. Pool is Clone, Send, and Sync — share it freely across tasks.
For most applications you will want a connection pool. See Connection pooling for configuration details.

Build docs developers (and LLMs) love