Rust SeaORM: Creating PostgreSQL Tables


3 min read

Rust SeaORM: Creating PostgreSQL Tables
You can find the source code for this tutorial right over here

Hey there! If you're looking to dive into creating tables with SeaORM in Rust, you've come to the right place. This post is all about making that process smooth and straightforward. Let's get started!

Introduction to SeaORM

Before we dive in, let's talk a bit about SeaORM. It's a fantastic tool for working with databases in Rust. It makes things like creating and managing tables a breeze. So, if you're working on a Rust project that needs database interaction, SeaORM is a tool you should definitely check out.

Setting Up Your Cargo Workspace

First things first, let's set up our Cargo workspace. If you already have a Cargo workspace, feel free to skip this part. Otherwise, we're going to create a new one and call it seaorm-by-example.

Create a new directory like this:

mkdir seaorm-by-example

Then, inside the seaorm-by-example directory, create a new Cargo.toml file with the following content:

members = [

This configuration is all about the Cargo workspace, and it does not include a [package] section. Here is where we'll add our migration crate.

Configuring the Database Connection

Next up, let's get our database connection ready. Create a .env file in the seaorm-by-example directory with your database details.


Creating the Migration Library Crate

Now, it's time to install sea-orm-cli and set up our migration library crate.

Install sea-orm-cli:

cargo install sea-orm-cli

Initialize the migration crate:

$ sea migrate init
Initializing migration directory...
Creating file `./migration/src/`
Creating file `./migration/src/`
Creating file `./migration/src/`
Creating file `./migration/Cargo.toml`
Creating file `./migration/`

After running this, your seaorm-by-example directory should look like this:

โ”œโ”€โ”€ Cargo.lock
โ”œโ”€โ”€ Cargo.toml
โ””โ”€โ”€ migration
    โ”œโ”€โ”€ Cargo.toml
    โ””โ”€โ”€ src

Don't forget to enable the necessary features in migration/Cargo.toml:

version = "0.12.0"
features = [

Generating New Migrations

To create a new migration file:

$ sea migrate generate "create user table"
Generating new migration...
Creating migration file `./migration/src/`
Adding migration `m20240106_061448_create_user_table` to `./migration/src/`

Update the generated file with your table specifications. Here's an example of what it might look like:

use sea_orm_migration::prelude::*;

pub struct Migration;

impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {

enum User {
    // We use iden="" to set the custom name for the table
    #[sea_orm(iden = "users")]

You can customize table or column names using #[sea_orm(iden = "")].

Running the Migration

Finally, it's time to run your migration and create the table in your database.

Run the migration:

$ sea migrate up
Applying all pending migrations
Applying migration 'm20240106_061448_create_user_table'
Migration 'm20240106_061448_create_user_table' has been applied

To use a custom schema, specify it when running the migration:

$ sea migrate up -s seaorm_by_example
Applying all pending migrations
Applying migration 'm20240106_061448_create_user_table'
Migration 'm20240106_061448_create_user_table' has been applied

And there you have it! Your table should now be all set up in your PostgreSQL database. Stay tuned for my next post where I'll cover CRUD operations on your new table. Happy coding!