1. Resources
  2. Shuttle Shared Databases

This plugin manages databases that are shared with other services on shuttle. Your database will share a postgres server with other users, but it will not be accessible by other users.

Usage

Add shuttle-shared-db to the dependencies for your service. Every type of shareable database is behind the following feature flag and attribute path

EngineFeature flagAttribute path
Postgrespostgresshuttle_shared_db::Postgres
MongoDBmongodbshuttle_shared_db::MongoDb

Parameters

ParameterTypeDefaultDescription
local_uristrNoneRather connect cargo shuttle run to this database and don’t spin up a Docker instance of the database

When setting the local_uri you can also insert secrets from Secrets.toml using string interpolation. To insert the PASSWORD secret, pass it in like this:

#[shuttle_service::main]
async fn poem(#[shuttle_shared_db::Postgres(
        local_uri = "postgres://postgres:{secrets.PASSWORD}@localhost:16695/postgres"
    )] pool: PgPool) -> shuttle_service::ShuttlePoem<impl poem::Endpoint> { ... }

Example

The shuttle poem main function below uses the [shuttle_shared_db::Postgres] attribute macro to provision a shared postgres database, which can be accessed using a pre-configured, authenticated sqlx Pool.

lib.rs
#[shuttle_service::main]
async fn poem(
    #[shuttle_shared_db::Postgres] pool: PgPool,
) -> shuttle_service::ShuttlePoem<impl poem::Endpoint> {
    pool.execute(include_str!("../schema.sql"))
        .await
        .map_err(CustomError::new)?;

    let app = Route::new()
        .at("/todo", post(add))
        .at("/todo/:id", get(retrieve))
        .with(AddData::new(pool));

    Ok(app)
}

The full example can be found on GitHub