Skip to content

PostgreSQL

import { Aside } from ‘@astrojs/starlight/components’;

PostgresStore wraps a sqlx connection pool and implements every repository trait. Enable it with the sqlx-postgres feature flag.

Cargo.toml
authx-storage = { path = "crates/authx-storage", features = ["sqlx-postgres"] }
use authx_storage::sqlx::PostgresStore;
let store = PostgresStore::connect("postgres://user:pass@localhost/mydb").await?;
// Run bundled migrations (idempotent — safe to call on every startup)
PostgresStore::migrate(&store.pool).await?;
postgres://username:password@host:port/database?sslmode=require
ComponentExampleNotes
UsernameauthxNeeds CREATE / SELECT / INSERT / UPDATE / DELETE
PasswordsecretURL-encode special chars
Hostdb.example.com
Port5432Default
Databasemyapp
SSL modesslmode=requireRequired in production

Migrations run automatically from the embedded SQL files:

  • 0001_initial_schema.sql — users, sessions, credentials, orgs, roles, memberships, audit_logs
  • 0002_.sql — api_keys, oauth_accounts, invites, username column

All tables are prefixed with authx_ to avoid conflicts with your own schema.

Terminal window
# Apply migrations without starting the server
authx migrate --database-url postgres://user:pass@host/db
# Start server with Postgres
DATABASE_URL=postgres://user:pass@host/db authx serve

The default pool has max_connections = 10. Adjust by constructing the pool directly:

use sqlx::postgres::PgPoolOptions;
let pool = PgPoolOptions::new()
.max_connections(25)
.connect(&database_url)
.await?;
let store = PostgresStore { pool };