Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting Error: PgEmbedError { error_type: PgInitFailure, source: None, message: None } when trying to run pg-embed #37

Open
manuelarte opened this issue Aug 13, 2024 · 2 comments

Comments

@manuelarte
Copy link

manuelarte commented Aug 13, 2024

Hi,

I am trying to use pg-embed to run a test. So I did the following:

cargo.toml

...
[dev-dependencies]
pg-embed = { version = "0.7", default-features = false, features = ["rt_tokio", "rt_tokio_migrate"] }

And my rs file:

#[cfg(test)]
mod tests {
    use std::path::PathBuf;
    use std::time::Duration;
    use pg_embed::pg_enums::PgAuthMethod;
    use pg_embed::pg_errors::PgEmbedError;
    use pg_embed::pg_fetch::{PG_V15, PgFetchSettings};
    use pg_embed::postgres::{PgEmbed, PgSettings};

    #[tokio::test]
    async fn test_pg_embed() -> Result<(), PgEmbedError> {
        /// Postgresql settings
        let pg_settings = PgSettings{
            // Where to store the postgresql database
            database_dir: PathBuf::from("data/db"),
            port: 5435,
            user: "postgres".to_string(),
            password: "password".to_string(),
            // authentication method
            auth_method: PgAuthMethod::Plain,
            // If persistent is false clean up files and directories on drop, otherwise keep them
            persistent: false,
            // duration to wait before terminating process execution
            // pg_ctl start/stop and initdb timeout
            // if set to None the process will not be terminated
            timeout: Some(Duration::from_secs(15)),
            // If migration sql scripts need to be run, the directory containing those scripts can be
            // specified here with `Some(PathBuf(path_to_dir)), otherwise `None` to run no migrations.
            // To enable migrations view the **Usage** section for details
            migration_dir: None,
        };

        /// Postgresql binaries download settings
        let fetch_settings = PgFetchSettings{
            version: PG_V15,
            ..Default::default()
        };

        // Create a new instance
        let mut pg = PgEmbed::new(pg_settings, fetch_settings).await?;

        // Download, unpack, create password file and database cluster
        pg.setup().await?;

        // start postgresql database
        pg.start_db().await?;

        // create a new database
        // to enable migrations view the [Usage] section for details
        pg.create_database("database_name").await?;

        // drop a database
        // to enable migrations view [Usage] for details
        pg.drop_database("database_name").await?;

        // check database existence
        // to enable migrations view [Usage] for details
        pg.database_exists("database_name").await?;

        // stop postgresql database
        pg.stop_db().await?;

        Ok(())
    }

}

But I get:

Error: PgEmbedError { error_type: PgInitFailure, source: None, message: None }

any idea?

@matthiasdebernardini
Copy link

Same issue here

@turingbuilder
Copy link

turingbuilder commented Sep 26, 2024

Hey @manuelarte, I started using pg-embed this week in one of our projects and it did work successfully. The version is a bit different. We chose to use the latest version which does not seem to be published to crates.io, instead the dependency looks like this:

[dependencies]
pg-embed = { git = "https://github.com/faokunega/pg-embed", tag = "v0.8.0",  default-features = false, features = ["rt_tokio"] }
tokio = { version = "1", features = [ "full", "fs" ] }

The only real difference in the Rust code is that for database_dir we use a fully qualified path to a specific the directory, rather than making it relative to the test executable.

In addition we chose to use sqlx to connect to the Postgres instance and do the database create and drop via normal SQL statements executed with sqlx. The connection pool setup looks like:

let pool = match PgPoolOptions::new().connect(&pg.db_uri).await {
    Ok(resp) => resp,
    Err(err) => {
        // Do something with error ...
    }
}

Here is the related issue that mentions how to use pg-embed 0.8.0 from GitHub rather than crates.io: Issue #28

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants