| Crates.io | pgmanager |
| lib.rs | pgmanager |
| version | 0.3.1 |
| created_at | 2025-12-21 12:29:56.910765+00 |
| updated_at | 2026-01-02 07:48:30.725891+00 |
| description | Provide locked access to test databases. |
| homepage | |
| repository | https://github.com/onelittle/pgmanager |
| max_upload_size | |
| id | 1997901 |
| size | 43,669 |
pgmanager is a utility for managing PostgreSQL databases in parallelized test environments, where tests are sharded across multiple processes. It provides mutually exclusive database assignment via a lightweight server exposed over a UNIX domain socket.
The primary goal is to give each test process exclusive access to a database instance, avoiding cross-test interference while remaining simple and fast.
cargo install pgmanager
[dev-dependencies]
pgmanager = "0.3.1"
{
inputs = {
pgmanager.url = "github:onelittle/pgmanager";
};
outputs = {
pgmanager,
...
}: {
devShell = pkgs.mkShell {
packages = [
pgmanager.packages.${system}.default
];
};
}
}
Conceptually, pgmanager is a tiny in-memory database (name) pool exposed over a UNIX socket.
Important constraints:
pgmanager does not reset database state.Database initialization can be made easier by using pgmanager wrap-each (see below).
Runs the pgmanager server independently. Clients connect via a UNIX socket and request a database.
Configuration is driven entirely by environment variables:
PGM_SOCKET – path to the UNIX socketPGM_DATABASE_PREFIX – database name prefixPGM_DATABASE_COUNT – number of databases in the pool# Serve a pool of 16 postgres databases
export PGM_DATABASE_PREFIX="myapp_test"
export PGM_DATABASE_COUNT="16"
export PGM_SOCKET="/tmp/pgm.sock"
pgmanager serve
#[cfg(test)]
mod tests {
#[tokio::test]
async fn test() {
let db_name = pgmanager::get_database().await;
eprintln!("A database is available at {}", db_name);
}
}
export PGM_SOCKET="/tmp/pgm.sock"
cargo test
Runs the server and client as one command. If specified, the PGM_SOCKET environment is used and passed to the subcommand. If no value is provided it will default to tmp/pgmanager.sock.
Rust integration is the same as above.
# Run tests with a pool of 16 postgres databases
export PGM_DATABASE_PREFIX="myapp_test"
export PGM_DATABASE_COUNT="16"
pgmanager wrap -- cargo test
Used to initialize and clean the test environment. Passes PGDATABASE to the subcommand. See pgmanager wrap-each --help for details.
# Create and drop 16 postgres databases
export PGM_DATABASE_PREFIX="myapp_test"
export PGM_DATABASE_COUNT="16"
pgmanager wrap-each -- createdb
pgmanager wrap-each --xargs -- dropdb
Transactions alone are sometimes insufficient for test isolation in parallel environments:
pgmanager enables stricter isolation (than just transactions) while still allowing parallel execution.