Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
deadpool postgres simple setup
use deadpool_postgres::tokio_postgres::NoTls;
use deadpool_postgres::{Config, ManagerConfig, Pool, PoolError, RecyclingMethod};
use once_cell::sync::Lazy;
use std::sync::Arc;
static DB_POOL: Lazy<Arc<Pool>> = Lazy::new(|| {
let mut cfg = Config::new();
cfg.dbname = Some(
std::env::var("DATABASE_URL")
.map_err(|_| String::from("Environment variable Database URL could not be read"))
.unwrap(),
);
cfg.manager = Some(ManagerConfig {
recycling_method: RecyclingMethod::Fast,
});
let pool = cfg.create_pool(NoTls).unwrap();
Arc::new(pool)
});
type ManagedObject = deadpool::managed::Object<
deadpool_postgres::ClientWrapper,
deadpool_postgres::tokio_postgres::error::Error,
>;
pub async fn db_conn() -> Result<ManagedObject, PoolError> {
DB_POOL.get().await
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment