# pg-embed
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.
Support for async-std and actix is planned and will be available soon.
# Usage
Add pg-embed to your Cargo.toml
Library without sqlx migration support
toml
# Cargo.toml
[dependencies]
pg-embed = { version = "0.4", default-features = false, features = ["rt_tokio"] }
Library with sqlx migration support
toml
# Cargo.toml
[dependencies]
pg-embed = "0.4"
A postgresql instance can be created using
PgEmbed::new(PgSettings, FetchSettings)
``` use pgembed::postgres::{PgEmbed, PgSettings}; use pgembed::fetch; use pgembed::fetch::{OperationSystem, Architecture, FetchSettings, PGV13}; use std::time::Duration;
/// Postgresql settings
let pgsettings = PgSettings{
/// Where to store the postgresql executables
executablesdir: PathBuf::from("data/postgres"),
/// Where to store the postgresql database
databasedir: PathBuf::from("data/db"),
port: 5432,
user: "postgres".tostring(),
password: "password".tostring(),
/// authentication method
authmethod: PgAuthMethod::Plain,
/// If persistent is false clean up files and directories on drop, otherwise keep them
persistent: false,
starttimeout: Duration::fromsecs(15),
/// If migration sql scripts need to be run, the directory containing those scripts can be
/// specified here with Some(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 fetchsettings = FetchSettings{ host: "https://repo1.maven.org".tostring(), operatingsystem: OperationSystem::Darwin, architecture: Architecture::Amd64, version: PGV13 };
/// Create a new instance let mut pg = PgEmbed::new(pgsettings, fetchsettings);
/// async block only to show that these methods need to be executed in an async context async { /// Download, unpack, create password file and database cluster pg.setup().await;
/// start postgresql database pg.start_db().await;
/// create a new database /// to enable migrations view the Usage section for details pg.createdatabase("databasename").await;
/// drop a new database /// to enable migrations view [Usage] for details /// to enable migrations view the Usage section for details pg.dropdatabase("databasename").await;
/// check database existence /// to enable migrations view [Usage] for details /// to enable migrations view the Usage section for details pg.databaseexists("databasename").await;
/// run migration sql scripts
/// to enable migrations view [Usage] for details
/// to enable migrations view the Usage section for details
pg.migrate("databasename").await;
};
/// get the base postgresql uri
/// postgres://{username}:{password}@localhost:{port}
let pguri: &str = &pg.db_uri;
/// get a postgresql database uri
/// postgres://{username}:{password}@localhost:{port}/{specified_database_name}
let pgdburi: String = pg.fulldburi("database_name");
/// stop postgresql database pg.stop_db();
```
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.