faokunega / pg-embed

Run a Postgresql database locally on Linux, MacOS or Windows as part of another Rust application or test.
MIT License
116 stars 26 forks source link

MacOS Monterey-M1 : Can't start DB => ""Could not find central directory end" #12

Closed AlexandreCantin closed 2 years ago

AlexandreCantin commented 2 years ago

When I am trying to launch the embed postgreSQL and I've got this error : InvalidArchive("Could not find central directory end")

Capture d’écran 2022-05-12 à 19 15 45

It seems that the setup does not wait the download to be finished before trying unzip it 😕 (the file in the $HOME/Library/Caches/pg-embed/darwin/arm64v8 is only 550 octets...)

For informations, I am using on macOS Monterey - Version 12.3.1 with a M1 Chip (not sure it's related but in case 🤷‍♂️)

Capture d’écran 2022-05-12 à 19 17 02

To reproduce it :

TOML file

[package]
name = "test2"
version = "0.1.0"
edition = "2021"

[dependencies]
pg-embed = "0.6.5"
tokio = { version = "1.17.0", features = ["full"] }

main.rs

use pg_embed::pg_enums::PgAuthMethod;
use pg_embed::pg_errors::PgEmbedError;
use pg_embed::postgres::{PgEmbed, PgSettings};

use pg_embed::pg_fetch::{PgFetchSettings, PG_V13};
use std::path::PathBuf;
use std::time::Duration;

#[tokio::main]
async fn main() {
    start().await.expect("Failed to start");
}

async fn start() -> Result<String, PgEmbedError> {
    let pg_settings = PgSettings {
        database_dir: PathBuf::from("data/db"),
        port: 5432,
        user: "postgres".to_string(),
        password: "password".to_string(),
        auth_method: PgAuthMethod::Plain,
        persistent: true,
        timeout: Some(Duration::from_secs(15)),
        migration_dir: None,
    };

    let fetch_settings = PgFetchSettings {
        version: PG_V13,
        ..Default::default()
    };

    // Create a new instance
    let mut pg = PgEmbed::new(pg_settings, fetch_settings)
        .await
        .expect("Failed to create new PgEmbed");

    // Download, unpack, create password file and database cluster
    pg.setup().await.expect("Failed to setup");

    // start postgresql database
    pg.start_db().await.expect("Failed to start db");

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

    // get a postgresql database uri
    // `postgres://{username}:{password}@localhost:{port}/{specified_database_name}`
    let pg_db_uri: String = pg.full_db_uri("database_name");

    Ok(pg_db_uri)
}

Thanks by advance for your answer and have a nice day 🙂

chanced commented 2 years ago

darwin-arm64v8-13.2.0.zip is actually:

<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
chanced commented 2 years ago

This is solved by changing the version.

    let fetch_settings = PgFetchSettings {
        version: pg_embed::pg_fetch::PostgresVersion("14.5.0"), // <-- this works
        ..Default::default()
    };