Skip to main content

Module postgres

Module postgres 

Source
Expand description

Public API for embedding and managing a PostgreSQL server.

The entry point is PgEmbed. A typical usage sequence is:

use pg_embed::postgres::{PgEmbed, PgSettings};
use pg_embed::pg_fetch::{PgFetchSettings, PG_V17};
use pg_embed::pg_enums::PgAuthMethod;
use std::path::PathBuf;
use std::time::Duration;

let pg_settings = PgSettings {
    database_dir: PathBuf::from("data/db"),
    port: 5432,
    user: "postgres".to_string(),
    password: "password".to_string(),
    auth_method: PgAuthMethod::Plain,
    persistent: false,
    timeout: Some(Duration::from_secs(15)),
    migration_dir: None,
};

let fetch_settings = PgFetchSettings { version: PG_V17, ..Default::default() };

let mut pg = PgEmbed::new(pg_settings, fetch_settings).await?;
pg.setup().await?;
pg.start_db().await?;

let uri = pg.full_db_uri("mydb");   // postgres://postgres:password@localhost:5432/mydb

pg.stop_db().await?;

Structsยง

PgEmbed
An embedded PostgreSQL server with full lifecycle management.
PgSettings
Configuration for a single embedded PostgreSQL instance.