| ← Back to README | Documentation Index | Quick Start | API Docs |
This guide walks you through setting up the authentication backend with SQLite and Prisma ORM.
Choose one of these options:
# Create and start PostgreSQL container
docker run --name privacy-analytics-db \
-e POSTGRES_USER=admin \
-e POSTGRES_PASSWORD=password123 \
-e POSTGRES_DB=analytics_db \
-p 5432:5432 \
-v postgres_data:/var/lib/postgresql/data \
-d postgres:16
# Verify it's running
docker ps
Connection String for .env:
DATABASE_URL="postgresql://admin:password123@localhost:5432/analytics_db"
# Install PostgreSQL
brew install postgresql@16
# Start PostgreSQL service
brew services start postgresql@16
# Create database and user
createdb analytics_db
psql analytics_db -c "CREATE USER admin WITH PASSWORD 'password123';"
psql analytics_db -c "ALTER USER admin CREATEDB;"
Connection String for .env:
DATABASE_URL="postgresql://admin:password123@localhost:5432/analytics_db"
# Install PostgreSQL
sudo apt update
sudo apt install postgresql postgresql-contrib
# Start service
sudo systemctl start postgresql
sudo systemctl enable postgresql
# Create database and user
sudo -u postgres createdb analytics_db
sudo -u postgres psql -c "CREATE USER admin WITH PASSWORD 'password123';"
sudo -u postgres psql -c "ALTER USER admin CREATEDB;"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE analytics_db TO admin;"
Connection String for .env:
DATABASE_URL="postgresql://admin:password123@localhost:5432/analytics_db"
Connection String for .env:
DATABASE_URL="postgresql://admin:password123@localhost:5432/analytics_db"
# Copy the example file
cp .env.example .env
# Edit .env and update DATABASE_URL
# DATABASE_URL="postgresql://admin:password123@localhost:5432/analytics_db"
# Install Prisma and required packages
npm install -D prisma @prisma/client
npm install bcryptjs jsonwebtoken express cors
npm install -D @types/bcryptjs @types/jsonwebtoken @types/cors
# Or with pnpm
pnpm add -D prisma @prisma/client
pnpm add bcryptjs jsonwebtoken express cors
pnpm add -D @types/bcryptjs @types/jsonwebtoken @types/cors
Prisma should auto-detect the existing schema, but run this to confirm:
# Generate Prisma client
npx prisma generate
# Optionally view the schema
npx prisma studio
# Run migrations to create tables
npx prisma migrate dev --name init
# If you get an error about existing tables, use:
npx prisma db push --force-reset
What this does:
prisma/schema.prisma.prisma/migrations/ folder with migration history# Open Prisma Studio (web UI to view database)
npx prisma studio
# This opens http://localhost:5555 where you can:
# - View all tables
# - Create, read, update, delete records
# - Test relationships
Create a test file test-db.ts:
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
async function main() {
// Test connection
const userCount = await prisma.user.count();
console.log(`✅ Connected to database. Users: ${userCount}`);
// Create a test user
const user = await prisma.user.create({
data: {
email: "test@example.com",
name: "Test User",
password: "hashed_password_here",
},
});
console.log("✅ Test user created:", user.id);
// Clean up
await prisma.user.delete({ where: { id: user.id } });
console.log("✅ Test user deleted");
}
main()
.catch(console.error)
.finally(() => prisma.$disconnect());
Run it:
npx tsx test-db.ts
Your database has these key tables:
Error: connect ECONNREFUSED 127.0.0.1:5432
Solution: Ensure PostgreSQL is running
# macOS
brew services list | grep postgresql
# Linux
sudo systemctl status postgresql
# Docker
docker ps | grep postgres
error: database "analytics_db" already exists
Solution: Drop and recreate
# Remove the container (Docker)
docker rm postgres-analytics
# Or drop database (native)
sudo -u postgres dropdb analytics_db
Prisma schema out of sync with database
Solution:
# Reset everything
npx prisma migrate reset
# Or force push schema
npx prisma db push --force-reset
Cannot find module '@prisma/client'
Solution:
npm install @prisma/client
npx prisma generate
Once setup is complete:
POST /api/v1/auth/register - User registrationPOST /api/v1/auth/login - User loginPOST /api/v1/auth/refresh - Token refresh# View database in web UI
npx prisma studio
# Generate Prisma client (after schema changes)
npx prisma generate
# Create a migration
npx prisma migrate dev --name <migration_name>
# Reset database (warning: deletes all data)
npx prisma migrate reset
# Seed database with initial data
npx prisma db seed
# Pull schema from existing database
npx prisma db pull
# Format Prisma schema
npx prisma format
For Development:
.env is fine.env in .gitignore (already done)For Production:
pwgen 32 1 or generate with openssl rand -base64 32Connection String Format (Production):
postgresql://user:password@host:port/database?sslmode=require&schema=public