SQLx’s PostgreSQL driver is written in pure Rust using zero unsafe code. It speaks the PostgreSQL wire protocol directly, supporting TLS, asynchronous notifications viaDocumentation 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.
LISTEN/NOTIFY, advisory locks, and the full range of PostgreSQL-native types including arrays, JSONB, UUID, ranges, and custom enums. The driver is runtime-agnostic and works with both tokio and async-std.
Feature flag
Enable the driver by adding thepostgres feature to your Cargo.toml. You must also choose a runtime and, if needed, a TLS backend.
Connecting
Connection URL
The simplest way to connect is with apostgres:// URL:
PgConnectOptions
For programmatic configuration, usePgConnectOptions. It reads standard PG* environment variables (PGHOST, PGPORT, PGUSER, PGPASSWORD, PGDATABASE) when constructed with PgConnectOptions::new().
Key options
| Option | Default | Description |
|---|---|---|
host | localhost (or Unix socket if available) | Hostname or path to a Unix domain socket directory |
port | 5432 | TCP port |
username | OS username | Login role |
password | None | Password (also read from ~/.pgpass) |
database | Same as username | Target database |
ssl_mode | Prefer | TLS negotiation mode (see below) |
application_name | None | Shown in pg_stat_activity |
statement_cache_capacity | 100 | LRU prepared-statement cache size per connection |
TLS and PgSslMode
PgSslMode controls TLS negotiation. Pass it to .ssl_mode() on PgConnectOptions or set sslmode= in the URL.
| Variant | Behaviour |
|---|---|
Disable | Never use TLS |
Allow | Try plain first; fall back to TLS |
Prefer | Try TLS first; fall back to plain (default) |
Require | Require TLS; verify CA if a root cert is present |
VerifyCa | Require TLS; verify the server certificate is signed by a trusted CA |
VerifyFull | Like VerifyCa, plus hostname verification |
Using
rustls requires TLS 1.2 or later. If you see HandshakeFailure when connecting to an older server, switch to tls-native-tls which uses the system TLS stack with broader version support.Connection pooling
PgPool (an alias for Pool<Postgres>) manages a pool of connections. Prefer PgPool over individual connections in application code.
- URL
- PgPoolOptions
LISTEN / NOTIFY
PgListener provides an async stream of server-sent notifications. It automatically reconnects on connection loss and re-subscribes to all channels.
Advisory locks
PostgreSQL advisory locks are application-level cooperative locks keyed by a 64-bit integer. They are useful for distributed mutual exclusion.PostgreSQL-specific types
Enable additional Cargo features to unlock type mappings:uuid
Maps
UUID ↔ uuid::Uuid. Enable with features = ["uuid"].json
Maps
JSON / JSONB ↔ serde_json::Value. Enable with features = ["json"].chrono
Maps
TIMESTAMP, DATE, TIME ↔ chrono types. Enable with features = ["chrono"].bigdecimal
Maps
NUMERIC ↔ bigdecimal::BigDecimal. Enable with features = ["bigdecimal"].Built-in type mapping
| PostgreSQL type | Rust type |
|---|---|
BOOL | bool |
SMALLINT / INT2 | i16 |
INTEGER / INT4 | i32 |
BIGINT / INT8 | i64 |
REAL / FLOAT4 | f32 |
DOUBLE PRECISION / FLOAT8 | f64 |
TEXT / VARCHAR / CHAR | String / &str |
BYTEA | Vec<u8> / &[u8] |
UUID | uuid::Uuid (feature: uuid) |
JSON / JSONB | serde_json::Value (feature: json) |
INET / CIDR | ipnetwork::IpNetwork (feature: ipnetwork) or ipnet::IpNet (feature: ipnet) |
TIMESTAMP | chrono::NaiveDateTime (feature: chrono) |
TIMESTAMPTZ | chrono::DateTime<Utc> (feature: chrono) |
DATE | chrono::NaiveDate (feature: chrono) |
NUMERIC | bigdecimal::BigDecimal (feature: bigdecimal) |
T[] (array) | Vec<T> |
Custom / enum types
Use#[derive(sqlx::Type)] to map a Rust enum to a PostgreSQL enum type:
WHERE IN with arrays
PostgreSQL supports binding aVec directly using = ANY($1), which avoids generating a different query plan for every possible array length:
!= ANY($1) is not equivalent to NOT IN (...). Use != ALL($1) for that behaviour.Bulk inserts with UNNEST
Pass multiple arrays toUNNEST() to perform efficient bulk inserts without generating combinatorially different query plans: