#postgresql #server #linux-macos #testing


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

27 releases

0.7.1 Dec 27, 2022
0.6.5 Nov 5, 2021
0.6.1 Jul 22, 2021
0.5.4 Jul 22, 2021
0.1.3 Mar 23, 2021

#408 in Database interfaces

Download history 176/week @ 2024-03-13 300/week @ 2024-03-20 93/week @ 2024-03-27 153/week @ 2024-04-03 217/week @ 2024-04-10 189/week @ 2024-04-17 157/week @ 2024-04-24 145/week @ 2024-05-01 185/week @ 2024-05-08 308/week @ 2024-05-15 478/week @ 2024-05-22 561/week @ 2024-05-29 780/week @ 2024-06-05 1100/week @ 2024-06-12 862/week @ 2024-06-19 1130/week @ 2024-06-26

4,069 downloads per month
Used in 6 crates (2 directly)




Crates.io Docs.rs Crates.io Crates.io

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

The currently supported async runtime for pg-embed is tokio.


  • Add pg-embed to your Cargo.toml

    Library without sqlx migration support

    # Cargo.toml
    pg-embed = { version = "0.7", default-features = false, features = ["rt_tokio"] }

    Library with sqlx migration support

    # Cargo.toml
    pg-embed = "0.7"


use pg_embed::postgres::{PgEmbed, PgSettings, PgAuthMethod};
use pg_embed::fetch;
use pg_embed::fetch::{PgFetchSettings, PG_V13};
use std::time::Duration;
use std::path::PathBuf;

/// Postgresql settings
let pg_settings = PgSettings{
    // Where to store the postgresql database
    database_dir: PathBuf::from("data/db"),
    port: 5432,
    user: "postgres".to_string(),
    password: "password".to_string(),
    // authentication method
    auth_method: PgAuthMethod::Plain,
    // If persistent is false clean up files and directories on drop, otherwise keep them
    persistent: false,
    // duration to wait before terminating process execution
    // pg_ctl start/stop and initdb timeout
    // if set to None the process will not be terminated
    timeout: Some(Duration::from_secs(15)),
    // If migration sql scripts need to be run, the directory containing those scripts can be
    // specified here with `Some(PathBuf(path_to_dir)), otherwise `None` to run no migrations.
    // To enable migrations view the **Usage** section for details
    migration_dir: None,

/// Postgresql binaries download settings
let fetch_settings = PgFetchSettings{
       version: PG_V13,

/// async block only to show that these methods need to be executed in an async context
async { 
    // Create a new instance
    let mut pg = PgEmbed::new(pg_settings, fetch_settings).await?;

    // Download, unpack, create password file and database cluster

    // start postgresql database

    // create a new database
    // to enable migrations view the [Usage] section for details

    // drop a database
    // to enable migrations view [Usage] for details

    // check database existence
    // to enable migrations view [Usage] for details

    // run migration sql scripts
    // to enable migrations view [Usage] for details
    // stop postgresql database
// get the base postgresql uri
    // `postgres://{username}:{password}@localhost:{port}`
    let pg_uri: &str = &pg.db_uri;

    // get a postgresql database uri
    // `postgres://{username}:{password}@localhost:{port}/{specified_database_name}`
    let pg_db_uri: String = pg.full_db_uri("database_name");


The downloaded postgresql binaries are cached in the following directories:

  • On Linux:




  • On Windows:


  • On MacOS:


Recent Breaking Changes

pg-embed follows semantic versioning, so breaking changes should only happen upon major version bumps. The only exception to this rule is breaking changes that happen due to implementation that was deemed to be a bug, security concerns, or it can be reasonably proved to affect no code. For the full details, see CHANGELOG.md.


pg-embed is licensed under the MIT license. Please read the LICENSE-MIT file in this repository for more information.


Reliant on the great work being done by zonkyio/embedded-postgres-binaries in order to fetch precompiled binaries from Maven.


~307K SLoC