#postgresql #cleanup #temp-dir #database-testing #fixtures #db-url

pgdb

Creates and runs Postgres databases through Rust in temporary directories, cleaned up on drop

7 releases (4 breaking)

0.5.0 Aug 2, 2025
0.4.0 Jul 4, 2025
0.3.0 Apr 9, 2024
0.2.0 Apr 1, 2024
0.1.2 Jun 15, 2021

#1234 in Database interfaces

Download history 123/week @ 2025-08-02 7/week @ 2025-08-09 2/week @ 2025-08-23 3/week @ 2025-08-30 19/week @ 2025-09-27 18/week @ 2025-10-04 13/week @ 2025-10-11 23/week @ 2025-10-18 17/week @ 2025-10-25

72 downloads per month
Used in pgdb_cli

MIT/Apache

35KB
710 lines

pgdb

A small Rust library to create and run ephemeral Postgres databases, typically used as unit test fixtures.

Quick start

Tests requiring a fresh database (but not cluster) instance can use db_fixture:

let db_url = pgdb::db_fixture();
// You can now use `db_url` in your ORM. The database will not be shut down before `db_url` is dropped.

Note that databases are not cleaned up until the testing process exits.

Requires that regular Postgres database utilities like postgres and initdb are available on the path at runtime.

Detailed usage

pgdb supports configuring and starting a Postgres database instance through a builder pattern, with cleanup on Drop:

let user = "dev";
let pw = "devpw";
let db = "dev";

// Run a postgres instance.
let pg = pgdb::Postgres::build()
    .start()
    .expect("could not build postgres database");

// We can now create a regular user and a database.
pg.as_superuser()
    .create_user(user, pw)
    .expect("could not create normal user");

pg.as_superuser()
    .create_database(db, user)
    .expect("could not create normal user's db");

// Now we can run DDL commands, e.g. creating a table.
let client = pg.as_user(user, pw);
client
    .run_sql(db, "CREATE TABLE foo (id INT PRIMARY KEY);")
    .expect("could not run table creation command");

Dependencies

~6–19MB
~244K SLoC