#binary #escaping

pgdb

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

3 releases

0.1.2 Jun 15, 2021
0.1.1 Jun 12, 2021
0.1.0 Jun 12, 2021

26 downloads per month
Used in pgdb_cli

MIT/Apache

17KB
325 lines

pgdb-rs

A small rust crate that allow easy creation and running of temporary Postgres databases, typically used for unit tests or similar things:

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

// Run a postgres instance on port `15432`.
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");

See the documentation for details.


lib.rs:

Runs Postgres instances.

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

Example

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

// Run a postgres instance on port `15432`.
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

~4–15MB
~176K SLoC