use std::env; use deadpool_postgres::Manager; use deadpool_postgres::ManagerConfig; use deadpool_postgres::Pool; use deadpool_postgres::PoolConfig; use deadpool_postgres::RecyclingMethod; use deadpool_postgres::Runtime; pub async fn get_db_client() -> deadpool_postgres::Object { let host = env::var("POSTGRES_HOST").unwrap_or("localhost".to_string()); let port = env::var("POSTGRES_PORT").unwrap_or("5432".to_string()); let db = env::var("POSTGRES_DB").unwrap_or("petro_test_db".to_string()); let user = env::var("POSTGRES_USER").unwrap_or("postgres".to_string()); let password = env::var("POSTGRES_PASSWORD").unwrap_or("".to_string()); // See https://docs.rs/tokio-postgres/0.7.10/tokio_postgres/config/struct.Config.html let mut pg_config = tokio_postgres::Config::new(); pg_config .host(&host) .port(port.parse().expect("Port should be a valid number")) .dbname(&db) .user(&user) .password(&password) .application_name("petro_test"); let manager_config = ManagerConfig { recycling_method: RecyclingMethod::Fast, }; let tls_connector = tokio_postgres::NoTls; let manager = Manager::from_config(pg_config, tls_connector, manager_config); // Each test will have their own pool let pool_config = PoolConfig::new(2); let pool = Pool::builder(manager) .config(pool_config) .runtime(Runtime::Tokio1) .build() .expect("Pool should succesfully build"); return pool.get().await.expect("Connection to PG should be OK"); }