Detailed guide for setting up PostgreSQL database for Apex CLI.
Apex CLI uses PostgreSQL with Prisma ORM for:
- User authentication (Better Auth)
- Session management
- Device authorization tokens
- Go to neon.tech
- Sign up with GitHub or email
- Verify your email
- Click "Create Project"
- Fill in:
- Project Name:
apex-cli - Region: Choose closest to you
- PostgreSQL Version: 16 (latest)
- Project Name:
- Click "Create Project"
- After creation, you'll see the connection details
- Click "Copy" on the connection string
- It looks like:
postgresql://USER:PASSWORD@ep-xxx.region.aws.neon.tech/neondb?sslmode=require
DATABASE_URL="postgresql://USER:PASSWORD@ep-xxx.region.aws.neon.tech/neondb?sslmode=require"- Go to supabase.com
- Sign up and create organization
- Click "New Project"
- Fill in:
- Name:
apex-cli - Database Password: Generate strong password (save it!)
- Region: Choose closest
- Name:
- Go to Settings → Database
- Scroll to "Connection string"
- Copy the URI format
- Replace
[YOUR-PASSWORD]with your database password
DATABASE_URL="postgresql://postgres:[YOUR-PASSWORD]@db.xxx.supabase.co:5432/postgres"- Go to railway.app
- Sign in with GitHub
- Click "New Project"
- Click "Add Service" → "Database" → "PostgreSQL"
- Wait for provisioning
- Click on the PostgreSQL service
- Go to "Connect" tab
- Copy "Postgres Connection URL"
DATABASE_URL="postgresql://postgres:xxx@containers-xxx.railway.app:5432/railway"Windows:
- Download from postgresql.org
- Run installer, remember the password you set
- Default port: 5432
macOS:
brew install postgresql@16
brew services start postgresql@16Linux (Ubuntu/Debian):
sudo apt update
sudo apt install postgresql postgresql-contrib
sudo systemctl start postgresql# Connect to PostgreSQL
psql -U postgres
# Create database
CREATE DATABASE apex_cli;
# Create user (optional)
CREATE USER apex_user WITH PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE apex_cli TO apex_user;
# Exit
\qDATABASE_URL="postgresql://postgres:your_password@localhost:5432/apex_cli"cd server
npx prisma generatenpx prisma db pushnpx prisma studioThis opens a browser to view your database tables.
Apex CLI creates these tables (managed by Better Auth):
| Table | Purpose |
|---|---|
user |
User accounts |
session |
Active sessions |
account |
OAuth provider links |
verification |
Email verification tokens |
deviceAuthorization |
CLI device codes |
-
Never commit
.envfiles# .gitignore .env .env.*
-
Use SSL in production
DATABASE_URL="...?sslmode=require"
-
Rotate credentials regularly
- Change database password every 90 days
- Update connection string in deployment
-
Use connection pooling for high load
- Neon: Pooler connection string available
- Supabase: Use port 6543 for pooling
Create a test script test-db.js:
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function main() {
try {
await prisma.$connect();
console.log('✅ Database connected successfully!');
const userCount = await prisma.user.count();
console.log(`📊 Users in database: ${userCount}`);
} catch (error) {
console.error('❌ Database connection failed:', error.message);
} finally {
await prisma.$disconnect();
}
}
main();Run:
node test-db.jsIf you modify prisma/schema.prisma:
# Create migration
npx prisma migrate dev --name add_new_field
# Apply migration to production
npx prisma migrate deploy| Issue | Solution |
|---|---|
Connection refused |
Check PostgreSQL is running |
SSL required |
Add ?sslmode=require to URL |
Authentication failed |
Verify username/password |
Database does not exist |
Create database first |
Prisma schema out of sync |
Run npx prisma db push |