Prerequisites
Before setting up the database, ensure you have:- PostgreSQL installed locally or access to a hosted instance (e.g., Supabase)
- Database credentials configured in
.env.local
Install PostgreSQL locally
If you don’t have PostgreSQL installed:Download PostgreSQL
Using Supabase (recommended)
For a managed PostgreSQL instance, you can use Supabase:Create a Supabase project
- Go to supabase.com
- Create a new project
- Wait for the database to be provisioned
Get connection string
- Navigate to Settings > Database
- Copy the Connection string under “Connection pooling”
- Replace
[YOUR-PASSWORD]with your database password
Run database migrations
Biovity’s database migrations are located in
lib/db/migrations/. The migration system is managed through Better Auth’s schema setup.Verify database connection
Test your database connection by checking if the connection string is valid:If the connection is successful, you’ll see the PostgreSQL version.
Run migrations
Better Auth automatically creates required tables on first run. Start the development server to initialize the schema:The authentication tables will be created automatically, including:
user- User accountssession- Active sessionsaccount- OAuth accounts- Custom fields:
type,profession,avatar,isActive,organizationId
Database architecture
Biovity uses a connection pooling strategy for PostgreSQL:- Connection pool - Shared connection pool in
lib/db/index.ts - Connection limits - Configured for optimal performance
- Idle timeout - Prevents stale connections
For production best practices, refer to the
docs/POSTGRES-BEST-PRACTICES.md file in the Biovity repository.Verify database setup
To verify your database is set up correctly:-
Check that tables were created:
-
Verify Better Auth tables exist:
usersessionaccount
Troubleshooting
Connection refused
If you see “connection refused” errors:- Verify PostgreSQL is running:
pg_isready - Check the port is correct (default: 5432)
- Ensure the host is accessible
Authentication failed
- Verify username and password in
DATABASE_URL - Check database exists:
psql -U postgres -l - Ensure user has proper permissions
SSL errors with Supabase
If you encounter SSL errors, append?sslmode=require to your connection string: