#postgresql #database-server #postgresql-embedded #embedded-server

pg-embed

Run a Postgresql database locally on Linux, MacOS or Windows as part of another Rust application or test

28 releases (1 stable)

Uses new Rust 2024

1.0.0 Feb 24, 2026
0.7.1 Dec 27, 2022
0.6.5 Nov 5, 2021
0.6.1 Jul 22, 2021
0.3.2 Mar 27, 2021

#90 in Database interfaces

Download history 491/week @ 2025-12-11 346/week @ 2025-12-18 81/week @ 2025-12-25 156/week @ 2026-01-01 452/week @ 2026-01-08 625/week @ 2026-01-15 860/week @ 2026-01-22 1179/week @ 2026-01-29 1834/week @ 2026-02-05 1260/week @ 2026-02-12 1754/week @ 2026-02-19 1810/week @ 2026-02-26 1593/week @ 2026-03-05 2504/week @ 2026-03-12 1497/week @ 2026-03-19 1488/week @ 2026-03-26

7,451 downloads per month
Used in 7 crates (3 directly)

MIT/Apache

310KB
1K SLoC

pg-embed logo

pg-embed

crates.io docs.rs downloads MSRV 1.88 license

Run a PostgreSQL server locally as part of a Rust application or test suite — no system installation required.

pg-embed banner


pg-embed downloads precompiled PostgreSQL binaries from zonkyio/embedded-postgres-binaries, caches them on first use, and manages the full server lifecycle (initdbpg_ctl startpg_ctl stop). Built on tokio.

Contents


Quick start

[dependencies]
pg-embed = "1.0"
use std::path::PathBuf;
use std::time::Duration;

use pg_embed::pg_enums::PgAuthMethod;
use pg_embed::pg_errors::Result;
use pg_embed::pg_fetch::{PgFetchSettings, PG_V18};
use pg_embed::postgres::{PgEmbed, PgSettings};

#[tokio::main]
async fn main() -> Result<()> {
    let mut pg = PgEmbed::new(
        PgSettings {
            database_dir:  PathBuf::from("data/db"),
            port:          5432,
            user:          "postgres".to_string(),
            password:      "password".to_string(),
            auth_method:   PgAuthMethod::MD5,
            persistent:    false,
            timeout:       Some(Duration::from_secs(30)),
            migration_dir: None,
        },
        PgFetchSettings { version: PG_V18, ..Default::default() },
    ).await?;

    pg.setup().await?;    // download + unpack + initdb (cached after first run)
    pg.start_db().await?;

    pg.create_database("mydb").await?;

    // postgres://postgres:password@localhost:5432/mydb
    let uri = pg.full_db_uri("mydb");
    println!("Connect at: {uri}");

    pg.stop_db().await?;
    Ok(())
}

Extensions

Third-party extensions (pgvector, PostGIS, etc.) are not included in the precompiled binaries. To install one, point install_extension() at a directory containing the pre-built files for your platform. Call it after setup() and before start_db().

use std::path::Path;

// After pg.setup() and before pg.start_db():
pg.install_extension(Path::new("extensions/pgvector")).await?;
pg.start_db().await?;

// Then activate it inside a database:
// CREATE EXTENSION IF NOT EXISTS vector;

Files are routed by extension:

File type Destination
.so, .dylib, .dll {cache}/lib/
.control, .sql {cache}/share/postgresql/extension/
Anything else Skipped

Pure-SQL extensions (no shared library) work the same way — simply omit the binary.


Features

Capability API Feature flag
🔄 Server lifecycle setup(), start_db(), stop_db() rt_tokio
🧩 Extension installation install_extension() rt_tokio
🗄️ Database management create_database(), drop_database(), database_exists() rt_tokio_migrate
🚀 Migrations migrate() rt_tokio_migrate

The default feature is rt_tokio_migrate (includes sqlx). For a smaller build without sqlx:

pg-embed = { version = "1.0", default-features = false, features = ["rt_tokio"] }

Additional behaviours included in all builds:

  • Binary caching — binaries are downloaded once per OS/arch/version and reused across runs.
  • Automatic shutdownpg_ctl stop is called on drop if the server is still running.
  • Concurrent safety — a global lock prevents duplicate downloads when multiple instances initialise simultaneously.

Platform support

OS Architectures
macOS amd64, arm64v8 ¹
Linux amd64, i386, arm32v6, arm32v7, arm64v8, ppc64le
Alpine Linux amd64, i386, arm32v6, arm32v7, arm64v8, ppc64le
Windows amd64, i386

Supported PostgreSQL versions: 10 – 18 (PG_V10PG_V18 constants).

¹ Apple Silicon binaries are available for PostgreSQL 14 and later only.


Binary cache

Binaries are stored at an OS-specific location and reused on subsequent runs:

OS Cache path
macOS ~/Library/Caches/pg-embed/
Linux $XDG_CACHE_HOME/pg-embed/ or ~/.cache/pg-embed/
Windows %LOCALAPPDATA%\pg-embed\

Documentation


License

pg-embed is dual-licensed under MIT / Apache 2.0.


Credits

Precompiled binaries provided by zonkyio/embedded-postgres-binaries, hosted on Maven Central.

Dependencies

~6–24MB
~307K SLoC