Skip to content

An Implementation of PostgreSQL's createdb, dropdb and other tools.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

YounessBird/pglit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

87 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pglit

CI TEST Unsafe forbidden Rust 1.57+

An implementation of PostgreSQL's createdb, dropdb and other tools.

This crate uses tokio-postgres to implement postgresql's tools such as createdb, dropdb. It also uses deadpool-postgres crate to support connection pooling.
To find all the available postgreSQL tools supported, check the API documentation.

Features

Feature Description Extra dependencies Default
quotes Enable support database name enclosed in double-quotes (") no no

Note that by default the database name shouldn't be enclosed in double quotes (").
To use a database that has a name enclosed in double-quotes ("), the quotes feature has to be enabled.

Example: create database using tokio_postgres::Config object

use tokio_postgres::{config::Config, NoTls};
use Pglit::create_db;

async fn create_the_db() {
    let mut config = Config::new();
    config.user("testuser");
    config.password("password");
    config.dbname("testdb");

    create_db(&mut config, "testdb", NoTls, |result| match result {
        Ok(_n) => println!("database successfully created"),
        Err(e) => println!("pg_error ,{:?}", e),
    })
    .await
}

Example: drop database using tokio_postgres::Config object

use tokio_postgres::{config::Config, NoTls};
use Pglit::drop_db;
async fn drop_the_db() {
    let mut config = Config::new();
    config.user("testuser");
    config.password("password");
    config.dbname("testdb");

    drop_db(&mut config, "testdb", NoTls, |result| match result {
        Ok(_n) => println!("database successfully dropped"),
        Err(e) => println!("pg_error ,{:?}", e),
    })
    .await
}

Example with deadpool-postgres, config and dotenv crates

# .env
PG.DBNAME=pglit
use Pglit::deadpool_create_db;

#[derive(serde::Deserialize, Debug)]
pub struct Config {
    pub pg: deadpool_postgres::Config,
}
impl Config {
    pub fn from_env() -> Result<Self, config::ConfigError> {
        ::config::Config::builder()
            .add_source(::config::Environment::default())
            .build()?
            .try_deserialize()
    }
}

async fn create_db_and_get_pool() {
    dotenv().ok();
    let cfg = Config::from_env().unwrap();
    let cfg = cfg.pg;

    let result = deadpool_create_db(cfg, None, NoTls).await;
    if let Ok(pool) = &result {
        let p = pool.get().await;
        match &p {
            Ok(_obj) => {
                println!("pool object created & returned");
            }
            Err(e) => {
                println!("error from pool {:?}", e);
            }
        };
    }
}

License

Licensed under either of

at your option.

About

An Implementation of PostgreSQL's createdb, dropdb and other tools.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages