pg_embed/lib.rs
1//!
2//! # pg-embed
3//!
4//! [](http://crates.io/crates/pg-embed)
5//! [](https://docs.rs/pg-embed)
6//! [](http://crates.io/crates/pg-embed)
7//! [](https://github.com/faokunega/pg-embed/blob/master/LICENSE)
8//!
9//! Run a Postgresql database locally on Linux, MacOS or Windows as part of another Rust application or test.
10//!
11//! The currently supported async runtime for **pg-embed** is [tokio](https://crates.io/crates/tokio).
12//!
13//! Support for [async-std](https://crates.io/crates/async-std) and [actix](https://crates.io/crates/actix) is planned
14//! and will be available soon.
15//!
16//! # Usage
17//!
18//! - Add pg-embed to your Cargo.toml
19//!
20//! *Library without sqlx migration support*
21//!
22//! ```toml
23//! # Cargo.toml
24//! [dependencies]
25//! pg-embed = { version = "0.9", default-features = false, features = ["rt_tokio"] }
26//! ```
27//!
28//! *Library with sqlx migration support*
29//!
30//! ```toml
31//! # Cargo.toml
32//! [dependencies]
33//! pg-embed = "0.9"
34//! ```
35//!
36//!
37//! # Examples
38//!
39//! ```
40//! use pg_embed::postgres::{PgSettings, PgEmbed};
41//! use pg_embed::pg_fetch::{PgFetchSettings, PG_V17};
42//! use pg_embed::pg_enums::PgAuthMethod;
43//! use std::time::Duration;
44//! use std::path::PathBuf;
45//! use pg_embed::pg_errors::Result;
46//!
47//! #[tokio::main]
48//! async fn main() -> Result<()> {
49//! /// Postgresql settings
50//! let pg_settings = PgSettings {
51//! // Where to store the postgresql database
52//! database_dir: PathBuf::from("data/db"),
53//! port: 5432,
54//! user: "postgres".to_string(),
55//! password: "password".to_string(),
56//! // authentication method
57//! auth_method: PgAuthMethod::Plain,
58//! // If persistent is false clean up files and directories on drop, otherwise keep them
59//! persistent: false,
60//! // duration to wait before terminating process execution
61//! // pg_ctl start/stop and initdb timeout
62//! // if set to None the process will not be terminated
63//! timeout: Some(Duration::from_secs(15)),
64//! // If migration sql scripts need to be run, the directory containing those scripts can be
65//! // specified here with `Some(PathBuf(path_to_dir)), otherwise `None` to run no migrations.
66//! // To enable migrations view the **Usage** section for details
67//! migration_dir: None,
68//! };
69//!
70//! /// Postgresql binaries download settings
71//! let fetch_settings = PgFetchSettings {
72//! version: PG_V17,
73//! ..Default::default()
74//! };
75//!
76//! // Use an async block that returns `Result`
77//! // Create a new instance
78//! let mut pg = PgEmbed::new(pg_settings, fetch_settings).await?;
79//!
80//! // Download, unpack, create password file and database cluster
81//! pg.setup().await?;
82//!
83//! // start postgresql database
84//! pg.start_db().await?;
85//!
86//! // create a new database
87//! // to enable migrations view the [Usage] section for details
88//! pg.create_database("database_name").await?;
89//!
90//! // drop a database
91//! // to enable migrations view [Usage] for details
92//! pg.drop_database("database_name").await?;
93//!
94//! // get the base postgresql uri
95//! // `postgres://{username}:{password}@localhost:{port}`
96//! let pg_uri: &str = &pg.db_uri;
97//!
98//! // get a postgresql database uri
99//! // `postgres://{username}:{password}@localhost:{port}/{specified_database_name}`
100//! let pg_db_uri: String = pg.full_db_uri("database_name");
101//!
102//! // check database existence
103//! // to enable migrations view [Usage] for details
104//! pg.database_exists("database_name").await?;
105//!
106//! // run migration sql scripts
107//! // to enable migrations view [Usage] for details
108//! pg.migrate("database_name").await?;
109//!
110//! // stop postgresql database
111//! pg.stop_db().await?;
112//!
113//! // Return success
114//! println!("PostgreSQL setup completed successfully!");
115//! Ok(())
116//! }
117//! ```
118//! ## Info
119//!
120//! The downloaded postgresql binaries are cached in the following directories:
121//!
122//! - On Linux:
123//!
124//! `$XDG_CACHE_HOME/pg-embed`
125//!
126//! or
127//!
128//! `$HOME/.cache/pg-embed`
129//! - On Windows:
130//!
131//! `{FOLDERID_LocalAppData}/pg-embed`
132//! - On MacOS:
133//!
134//! `$HOME/Library/Caches/pg-embed`
135//!
136//!
137//! ## Recent Breaking Changes
138//!
139//! 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](https://github.com/faokunega/pg-embed/blob/master/CHANGELOG.md).
140//!
141//!
142//!
143//! ## License
144//!
145//! pg-embed is licensed under the MIT license. Please read the [LICENSE-MIT](https://github.com/faokunega/pg-embed/blob/master/LICENSE) file in this repository for more information.
146//!
147//! # Notes
148//!
149//! Reliant on the great work being done by [zonkyio/embedded-postgres-binaries](https://github.com/zonkyio/embedded-postgres-binaries) in order to fetch precompiled binaries from [Maven](https://mvnrepository.com/artifact/io.zonky.test.postgres/embedded-postgres-binaries-bom).
150//!
151
152#[cfg(not(any(feature = "rt_tokio_migrate", feature = "rt_tokio",)))]
153compile_error!("one of the features ['rt_tokio_migrate', 'rt_tokio'] must be enabled");
154
155pub mod command_executor;
156pub mod pg_access;
157pub mod pg_commands;
158pub mod pg_enums;
159pub mod pg_errors;
160pub mod pg_fetch;
161pub mod pg_types;
162pub mod pg_unpack;
163pub mod postgres;