- You will need to add a
.env
file in the root directory with the following structure:
POSTGRES_PORT=5432
POSTGRES_USER="postgres"
POSTGRES_PASSWORD="postgres"
POSTGRES_DB="postgres"
DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres-database:${POSTGRES_PORT}/${POSTGRES_DB}"
-
Docker Compose
docker compose build
docker compose up
-
API Base URL: http://localhost:3000/api
-
Database Viewer: http://localhost:8081/
-
If not using Docker Compose:
watchexec --restart --watch src --exts rs -- cargo run
- Note: Database Service is Required for app to launch. Make sure to either manually launch + configure the database, or comment out all database relevant code.
- Note: this command requires the installation of
watchexec-cli
- Use this command to add it:
cargo install --locked watchexec-cli
cargo new axum-pg-docker-crud
cargo add axum
cargo add tokio --features=full
cargo add serde --features=derive
cargo add chrono
cargo add diesel --features=chrono,postgres
cargo add diesel-async --features bb8,postgres
NOTE:
- Diesel has its own CLI tool that is a standalone binary. That means it's not a dependency of the project, and doesn't impact the project's code at all.
- I opted to install it by following the directions here: Installing Diesel CLI
Diesel CLI Setup Steps
diesel setup --database-url='postgresql://postgres:postgres@localhost/postgres'
- This creates a root level
/migrations
folder - Note, that Database URL required @localhost instead of the
compose.yaml
defined service. This will require an adjustment (perhaps setting the database URL as a computed value on a struct?)
- This creates a root level
- I editted the autogenerated
diesel.toml
file and set theschema.rs
location to be within the/src/database
directory. diesel migration generate create_identities
- This migration was named
create_identities
because I had already set identities as an API namespace. - You need to write the SQL for these Migrations by hand in the
/migrations
directory.
- This migration was named
- Apply the written SQL Migrations:
diesel migration run
- I went to
http://localhost:8081/
, refreshed the page, and checked the tables.- I verified the
identities
table was made and queryable from the Web GUI.
- I verified the
- In following the Diesel tutorial, I ran
diesel migration redo
to verify thedown.sql
file correctly rolled back migrations. - I also verified my config changes to
diesel.toml
were reflected in the/src/database/schema.rs
file - Next, I added
/src/database/schema.rs
into/src/database/mod.rs
- I created a
models.rs
file in/src/database