#!/bin/bash echo "🚀 Setting up local PostgreSQL database for Portfolio..." # Check if PostgreSQL is installed if ! command -v psql &> /dev/null; then echo "📦 PostgreSQL not found. Installing..." # Detect OS and install PostgreSQL if [[ "$OSTYPE" == "linux-gnu"* ]]; then # Ubuntu/Debian if command -v apt-get &> /dev/null; then sudo apt-get update sudo apt-get install -y postgresql postgresql-contrib # CentOS/RHEL elif command -v yum &> /dev/null; then sudo yum install -y postgresql postgresql-server postgresql-contrib sudo postgresql-setup initdb sudo systemctl enable postgresql sudo systemctl start postgresql # Arch Linux elif command -v pacman &> /dev/null; then sudo pacman -S postgresql sudo -u postgres initdb -D /var/lib/postgres/data sudo systemctl enable postgresql sudo systemctl start postgresql else echo "❌ Unsupported Linux distribution. Please install PostgreSQL manually." exit 1 fi elif [[ "$OSTYPE" == "darwin"* ]]; then # macOS if command -v brew &> /dev/null; then brew install postgresql brew services start postgresql else echo "❌ Homebrew not found. Please install Homebrew first: https://brew.sh/" exit 1 fi else echo "❌ Unsupported OS. Please install PostgreSQL manually." exit 1 fi else echo "✅ PostgreSQL already installed" fi # Start PostgreSQL service if [[ "$OSTYPE" == "linux-gnu"* ]]; then sudo systemctl start postgresql elif [[ "$OSTYPE" == "darwin"* ]]; then brew services start postgresql fi # Create database and user echo "🔧 Setting up database..." sudo -u postgres psql -c "CREATE DATABASE portfolio_db;" 2>/dev/null || echo "Database already exists" sudo -u postgres psql -c "CREATE USER portfolio_user WITH PASSWORD 'portfolio_pass';" 2>/dev/null || echo "User already exists" sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE portfolio_db TO portfolio_user;" 2>/dev/null || echo "Privileges already granted" sudo -u postgres psql -c "ALTER USER portfolio_user WITH SUPERUSER;" 2>/dev/null || echo "Superuser already granted" # Create .env.local file echo "📝 Creating environment file..." cat > .env.local << EOF # Database Configuration DATABASE_URL="postgresql://portfolio_user:portfolio_pass@localhost:5432/portfolio_db?schema=public" # Next.js Configuration NEXTAUTH_SECRET="$(openssl rand -base64 32)" NEXTAUTH_URL="http://localhost:3000" # Optional: Analytics GOOGLE_ANALYTICS_ID="" GOOGLE_TAG_MANAGER_ID="" EOF echo "✅ Environment file created: .env.local" # Install dependencies echo "📦 Installing dependencies..." npm install # Generate Prisma client echo "🔧 Generating Prisma client..." npx prisma generate # Run database migrations echo "🗄️ Running database migrations..." npx prisma db push # Seed database with sample data echo "🌱 Seeding database with sample data..." npx prisma db seed echo "🎉 Database setup complete!" echo "" echo "📋 Next steps:" echo "1. Start your development server: npm run dev" echo "2. Visit http://localhost:3000/admin to manage projects" echo "3. Your database is running at localhost:5432" echo "" echo "🔧 Database commands:" echo "- View database: npx prisma studio" echo "- Reset database: npx prisma db push --force-reset" echo "- Generate client: npx prisma generate"