When Rust meets PostgreSQL

Elephantry is an open source database access framework in Rust dedicated to PostgreSQL.

Get started »

let elephantry = elephantry::Pool::new(&database_url)?;
let results = elephantry
    .execute("select name from department")?;

for result in &results {
    let name: String = result.get("name");
    println!("{}", name);

Simple query

Connect to the database, execute a query a tell witch data you want and its type. Elephantry does the rest!

Read more »

Complex queries

You can write queries as complex as you like.

Read more »

let elephantry = elephantry::Pool::new(database_url)?;
let results = elephantry
    .query::<(String, String)>(include_str!("query.sql"), &[])?;

for result in results {
    println!("{:?}", result);
with ranking as (
    select department.name as department,
        employee.first_name || ' ' || employee.last_name as name,
        rank() over (partition by department order by day_salary)
    from employee
    join department
        on department.department_id = employee.department_id
    order by department, rank
select row(department, name) from ranking
#[derive(Debug, elephantry::Entity)]
struct Employee {
    id: i32,
    name: String,

let employees = elephantry
    .query::<Employee>("select id, name from employee", &[])?;

for employee in employees {


Instead of retreive fields one by one, you can aggregate a row of result in a struct.

Read more »

Write data

You can easily retreive, create, update or delete entity with the model approach.

The cli helps you to generate entities from your database schema.

Read more »

#[elephantry(model = "Model", structure = "Structure")]
struct Entity {
    id: i32,
    name: String,

elephantry.find_all::<Model>(Some("order by name"))?;
elephantry.update_one::<Model>(pk!{id => entity.id}, entity)?;
// config-support
let mut config = config::Config::new();

let elephantry =
// r2d2
let manager =
# rocket
url = "postgresql://localhost/elcaro"


By enabling the corresponding feature (config-support, r2d2 or rocket), elephantry can easily integrate the rust ecosystem.


Elephantry supports async queries and have a transaction helper.

Read more »

let r#async = elephantry.r#async();
r#async.execute("select * from department").await?;
let transaction = elephantry.transaction();
// …