This guide will walk you through setting up the API web server with PostgreSQL database integration.
The easiest way to run the API server and database is using Docker Compose.
- Docker
- Docker Compose (usually included with Docker Desktop)
To connect to the PostgreSQL database running in Docker:
# Using docker compose exec
docker compose exec postgres psql -U ltx_user -d ltx_db
# Or using psql on your host (if installed)
psql -h localhost -U ltx_user -d ltx_db
# Password: ltx_passwordIf you prefer to run services manually without Docker, follow the instructions below.
- Rust toolchain (cargo, rustc)
- PostgreSQL 12 or higher
- diesel_cli (installation covered below)
brew install postgresql@15
brew services start postgresql@15sudo apt update
sudo apt install postgresql postgresql-contrib
sudo systemctl start postgresql
sudo systemctl enable postgresqlsudo pacman -S postgresql
sudo systemctl start postgresql
sudo systemctl enable postgresqlCreate the database and user:
# Connect to PostgreSQL as the postgres user
sudo -u postgres psql
# Run these commands in the PostgreSQL prompt:
CREATE USER ltx_user WITH PASSWORD 'ltx_password';
CREATE DATABASE ltx_db OWNER ltx_user;
GRANT ALL PRIVILEGES ON DATABASE ltx_db TO ltx_user;
\qNote: For production environments, use a strong password and consider using environment-specific credentials.
Install the Diesel CLI tool with PostgreSQL support:
cargo install diesel_cli --no-default-features --features postgresIf you encounter linking errors on macOS, you may need to set the PostgreSQL library path:
# For Homebrew PostgreSQL
export PQ_LIB_DIR="$(brew --prefix postgresql@15)/lib"
cargo install diesel_cli --no-default-features --features postgresNavigate to the api-ltx directory and run migrations:
cd src/api-ltx
diesel migration runThis will create the names table with a single name column.
To verify the migration worked:
diesel migration listThe API server can be configured using environment variables:
DATABASE_URL: PostgreSQL connection string (required)HOST: Host to bind to (default:127.0.0.1, use0.0.0.0for Docker)PORT: Port to listen on (default:3000)RUST_LOG: Logging level (default:api-ltx=debug,tower_http=debug)
If port 3000 or 5432 is already in use, you can change them in docker-compose.yml:
services:
postgres:
ports:
- "5433:5432" # Change host port to 5433
api:
ports:
- "8080:3000" # Change host port to 8080docker compose logs api
docker compose logs postgresdocker compose down -v
docker compose build --no-cache
docker compose upEnsure PostgreSQL is running:
# macOS
brew services list
# Linux
sudo systemctl status postgresqlIf migrations fail, you can reset the database:
diesel migration redoEnsure ~/.cargo/bin is in your PATH:
export PATH="$HOME/.cargo/bin:$PATH"Install cargo-watch for automatic recompilation:
cargo install cargo-watch
cargo watch -x 'run -p api-ltx'Using Docker:
docker compose exec postgres psql -U ltx_user -d ltx_db
\dt # List tables
SELECT * FROM names; # View all names
\q # QuitUsing local PostgreSQL:
psql -U ltx_user -d ltx_db
\dt # List tables
SELECT * FROM names; # View all names
\q # QuitUsing Docker:
docker compose down -v # Removes volumes
docker compose upUsing manual setup:
diesel migration revert --all
diesel migration run