Skip to content

Instantly share code, notes, and snippets.

@Enigo
Created January 23, 2023 10:56
Show Gist options
  • Save Enigo/3ba68f069ed0d5a3820d9cefada13dad to your computer and use it in GitHub Desktop.
Save Enigo/3ba68f069ed0d5a3820d9cefada13dad to your computer and use it in GitHub Desktop.
pub async fn open_connection() -> Pool<Postgres> {
let options = PgConnectOptions::new()
.host(env::var("DB_HOST").expect("DB_HOST should be set").as_str())
.port(env::var("DB_PORT").expect("DB_PORT should be set").parse().expect("DB_PORT should be a valid u16 value"))
.database(env::var("DB_DATABASE").expect("DB_DATABASE should be set").as_str())
.username(env::var("DB_USERNAME").expect("DB_USERNAME should be set").as_str())
.password(env::var("DB_PASSWORD").expect("DB_PASSWORD should be set").as_str())
.disable_statement_logging()
.clone();
PgPoolOptions::new()
.max_connections(5)
.connect_with(options)
.await
.expect("DB is not accessible!")
}
pub async fn close_connection(pool: Pool<Postgres>) {
pool.close().await;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment