This document walks you through the steps to get your database running.
-
Check if you have Node.js
node --version
- If you see
v18.0.0or higher: Great! - If you see "command not found": Install from https://nodejs.org
- If you see
-
Choose your database
- MongoDB - Best for flexible schemas and rapid prototyping (document-based, NoSQL)
- PostgreSQL - Recommended for most projects (advanced features, JSONB support)
- MySQL - Good for traditional web apps (simple, widely supported)
Not sure? Use PostgreSQL.
-
Choose cloud or local
- Cloud (Recommended): No installation, works anywhere, free tier available
- Local: Runs on your computer, requires Docker
Open your terminal and type:
# Go to your Documents folder (or wherever you want your project)
cd ~/Documents
# Create a folder for your project
mkdir my-hack-brown-project
# Go into that folder
cd my-hack-brown-project
# Verify you're in the right place
pwdYou should see something like: /Users/yourname/Documents/my-hack-brown-project
# Create all the folders you'll need
mkdir -p database config models routes controllers middleware scripts
# Verify the folders were created
ls -laYou should see: database, config, models, routes, controllers, middleware, scripts
IMPORTANT: You need to copy files from this starter kit to your project.
From the database-starter/postgresql/ folder, copy these files:
-
Copy to your project root:
package.json→my-hack-brown-project/package.json.env.example→my-hack-brown-project/.env.example.gitignore→my-hack-brown-project/.gitignore
-
Copy to config/ folder:
database.js→my-hack-brown-project/config/database.js
-
Copy to models/ folder:
userModel.js→my-hack-brown-project/models/userModel.jsprojectModel.js→my-hack-brown-project/models/projectModel.js
-
Copy to database/ folder:
schema.sql→my-hack-brown-project/database/schema.sqlseed.sql→my-hack-brown-project/database/seed.sql
-
Copy to scripts/ folder:
testConnection.js→my-hack-brown-project/scripts/testConnection.js
Verify you copied everything:
ls -la config/
ls -la models/
ls -la database/
ls -la scripts/
ls -laYou should see the files listed above.
npm installWhat you'll see:
- Lots of text scrolling by
- "added X packages" at the end
- A new
node_modules/folder appears
If you see an error:
- Make sure you copied
package.jsonto your project root - Make sure you're in the project folder (
pwdto check)
OPTION A: Cloud Database (Recommended)
-
Go to Supabase
- Open https://supabase.com in your browser
- Click "Start your project"
- Sign up with GitHub or email
-
Create a Project
- Click "New project"
- Choose a name:
hackatbrown-yourname - Choose a password
- Choose a region (closest to you)
- Click "Create new project"
- This takes 1-2 minutes. You'll see "Setting up project..."
-
Get Your Connection String
- Once ready, click "Connect" (top right)
- Click "App Frameworks"
- Under "Node.js", copy the connection string
- It looks like:
postgresql://postgres:[YOUR-PASSWORD]@db.abc123.supabase.co:5432/postgres
-
Save It to Your Project
# Create your .env file cp .env.example .env # Open .env in your text editor (VS Code, Sublime, nano, etc.) nano .env
Replace the
DATABASE_URLline with YOUR connection string:DATABASE_URL=postgresql://postgres:[YOUR-PASSWORD]@db.abc123.supabase.co:5432/postgres PORT=3000Save and close (in nano: Ctrl+X, then Y, then Enter)
OPTION B: Local Database with Docker
-
Make sure Docker is running
docker --version
If you see an error, install Docker Desktop from https://docker.com
-
Start PostgreSQL
docker run -d \ --name postgres-hackatbrown \ -e POSTGRES_PASSWORD=hackatbrown123 \ -e POSTGRES_DB=hackatbrown \ -e POSTGRES_USER=hackatbrown \ -p 5432:5432 \ postgres:15
-
Create .env file
cp .env.example .env echo "DATABASE_URL=postgresql://hackatbrown:hackatbrown123@localhost:5432/hackatbrown" > .env echo "PORT=3000" >> .env
npm run test-connectionWhat you SHOULD see:
Testing PostgreSQL connection...
[+] PostgreSQL Connected Successfully!
Database: hackatbrown (or your database name)
Server Time: 2024-11-04 ...
Version: PostgreSQL 15
[+] Connection test passed!
You can now run: npm run db:setup
If you see "Connection test failed":
- Check your .env file exists:
cat .env - Check the DATABASE_URL is correct
- For cloud: Wait 1 more minute (database might still be starting)
- For local: Check Docker is running:
docker ps
npm run db:setupWhat you SHOULD see:
NOTICE: [+] Database schema created successfully!
NOTICE: Tables: users, projects, project_collaborators
NOTICE: Indexes created for optimal performance
NOTICE: Triggers set up for automatic updates
...
NOTICE: [+] Sample data inserted successfully!
NOTICE: Users created: 5
NOTICE: Projects created: 8
This command:
- Creates 3 tables
- Adds indexes for speed
- Adds 5 sample users
- Adds 8 sample projects
If you see an error:
- Make sure
npm run test-connectionworked first - Check that schema.sql and seed.sql are in the
database/folder
You need a server.js file to run the API. Create it:
nano server.jsPaste this code:
// Load environment variables from .env file
require('dotenv').config();
// Import dependencies
const express = require('express');
const cors = require('cors');
const { testConnection } = require('./config/database');
// Create Express app
const app = express();
// Middleware
app.use(cors());
app.use(express.json());
// Welcome route
app.get('/', (req, res) => {
res.json({
message: 'Hack@Brown Database API',
endpoints: {
users: '/api/users',
projects: '/api/projects'
}
});
});
// Health check
app.get('/health', async (req, res) => {
const dbConnected = await testConnection();
res.json({
status: 'ok',
database: dbConnected ? 'connected' : 'disconnected'
});
});
// Example user routes (you'll expand this)
app.get('/api/users', async (req, res) => {
try {
const userModel = require('./models/userModel');
const users = await userModel.getAllUsers();
res.json(users);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Example project routes (you'll expand this)
app.get('/api/projects', async (req, res) => {
try {
const projectModel = require('./models/projectModel');
const projects = await projectModel.getAllProjects();
res.json(projects);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Start server
const PORT = process.env.PORT || 3000;
app.listen(PORT, async () => {
console.log(`Server running on port ${PORT}`);
await testConnection();
});Save and close (Ctrl+X, Y, Enter)
npm startWhat you SHOULD see:
Server running on port 3000
[+] PostgreSQL Connected Successfully!
Database: hackatbrown
If port 3000 is in use:
- Edit .env:
PORT=3001 - Run
npm startagain
Open your web browser and visit:
-
- You should see:
{"message":"Hack@Brown Database API",...}
- You should see:
-
http://localhost:3000/api/users
- You should see JSON with 5 users (johndoe, janesmith, etc.)
-
http://localhost:3000/api/projects
- You should see JSON with 8 projects
SUCCESS! Your database is working!
my-hack-at-brown-project/
├── .env # YOUR database password (never commit!)
├── .gitignore # Tells git to ignore .env
├── package.json # Lists dependencies
├── server.js # YOUR API server
├── node_modules/ # Installed packages (never edit)
│
├── database/
│ ├── schema.sql # Creates tables (you ran this once)
│ └── seed.sql # Sample data (you ran this once)
│
├── config/
│ └── database.js # Database connection code
│
├── models/
│ ├── userModel.js # Functions to work with users
│ └── projectModel.js # Functions to work with projects
│
└── (you can add:)
├── routes/ # Organize your API endpoints
├── controllers/ # Business logic
└── middleware/ # Error handling, auth, etc.
-
server.js - Your main file
- Loads environment variables (.env)
- Sets up Express server
- Defines API endpoints
- Starts listening on port 3000
-
config/database.js - Database connection
- Connects to PostgreSQL
- Exports a
query()function - You use this to run SQL
-
models/userModel.js - User operations
getAllUsers()- Get all usersgetUserById(id)- Get one usercreateUser(data)- Add a userupdateUser(id, data)- Change a userdeleteUser(id)- Remove a user- Each function uses
query()from database.js
-
models/projectModel.js - Project operations
- Similar to userModel but for projects
Browser Request
↓
server.js (endpoint: /api/users)
↓
models/userModel.js (calls getAllUsers())
↓
config/database.js (runs SQL query)
↓
PostgreSQL Database
↓
Returns data back up the chain
↓
Browser sees JSON response
# Get all users
curl http://localhost:3000/api/users
# Get all projects
curl http://localhost:3000/api/projects
# Create a new user
curl -X POST http://localhost:3000/api/users \
-H "Content-Type: application/json" \
-d '{"username":"testuser","email":"test@test.com","full_name":"Test User"}'Edit server.js to add more routes:
// Get a single user by ID
app.get('/api/users/:id', async (req, res) => {
try {
const userModel = require('./models/userModel');
const user = await userModel.getUserById(req.params.id);
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
res.json(user);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Create a new user
app.post('/api/users', async (req, res) => {
try {
const userModel = require('./models/userModel');
const newUser = await userModel.createUser(req.body);
res.status(201).json(newUser);
} catch (error) {
res.status(500).json({ error: error.message });
}
});As your API grows, organize it better:
- Create route files (routes/users.js, routes/projects.js)
- Create controllers (controllers/userController.js)
- Add middleware (middleware/auth.js for authentication)
Solution:
npm installMake sure you ran this after copying package.json
Solution:
# Check if .env file exists
ls -la .env
# If not, create it
cp .env.example .env
# Edit it and add your DATABASE_URL
nano .envSolution:
# Edit .env and change the port
echo "PORT=3001" >> .env
# Or stop the other process
lsof -ti:3000 | xargs killSolution: You didn't run the database setup:
npm run db:setupFor Cloud (Supabase):
- Check your connection string in .env
- Make sure you replaced
[YOUR-PASSWORD]with your actual password - Wait 1-2 minutes (database might still be starting)
For Local (Docker):
- Check Docker is running:
docker ps - If you don't see
postgres-hackatbrown, start it again - Check the connection string matches what you set
What to build next:
- Add authentication (JWT tokens, password hashing)
- Add more endpoints (update, delete, search)
- Build a frontend (React, Vue, etc.)
- Deploy to production (Heroku, Vercel, Railway)
Resources:
- PostgreSQL Guide:
postgresql/PostgreSQL_Starter.md - Model Documentation: Check the comments in
models/userModel.js - Troubleshooting: See Part 5 above
If you chose MySQL instead:
- Use the
mysql/folder instead ofpostgresql/ - Docker command is different:
docker run -d \ --name mysql-hackatbrown \ -e MYSQL_ROOT_PASSWORD=hackatbrown123 \ -e MYSQL_DATABASE=hackatbrown \ -e MYSQL_USER=hackatbrown \ -e MYSQL_PASSWORD=hackatbrown123 \ -p 3306:3306 \ mysql:8.0
- Connection string format:
DATABASE_URL=mysql://hackatbrown:hackatbrown123@localhost:3306/hackatbrown