Search code examples
databasesqliterustrust-diesel

How do I create a new database using Diesel?


Is it possible to create a new database in Rust using Diesel? I can't find anything in the documentation. I am specifically interested in SQLite.


Solution

  • TBH, I didn't really search in the documentation but, on the Cargo.toml side, you need :

    [dependencies]
    diesel = { version = "1.4.3", features = ["sqlite"] }
    

    Inside you .env file, you can specify the path to the database file like :

    DATABASE_URL=mydb.sqlite3
    

    If you follow the "Getting Started" guide, then, you just need to use SqliteConnection instead of PgConnection :

    use diesel::sqlite::SqliteConnection;
    
    // ...
    
    pub fn establish_connection() -> SqliteConnection {
        // ...
    }