13 KiB
Supabase Setup - Official Method (setup2.sh)
📖 Overview
setup2.sh เป็น setup script ที่ทำตามขั้นตอนจาก Official Supabase Self-Hosting Guide อย่างเคร่งครัด
🔄 ความแตกต่างระหว่าง setup.sh และ setup2.sh
| Feature | setup.sh (Custom) | setup2.sh (Official) |
|---|---|---|
| แหล่งที่มา | ดาวน์โหลด config files แยก | Clone official Supabase repo |
| docker-compose.yml | Custom version | Official version (latest) |
| การ generate secrets | Manual instructions | ใช้ official generate-keys.sh |
| การอัปเดต | Manual | ตาม official releases |
| Port configuration | Pre-configured | Auto-adjusted |
| แนะนำสำหรับ | Quick setup, custom config | Production, official support |
✅ Prerequisites
ต้องติดตั้งก่อนรัน script:
-
Git - สำหรับ clone repository
git --version -
Docker & Docker Compose
docker --version docker compose version -
OpenSSL - สำหรับ generate secrets (มักติดตั้งมาแล้ว)
openssl version
🚀 วิธีใช้งาน
1. รัน Setup Script
cd 02-supabase
bash setup2.sh
Script จะทำสิ่งต่อไปนี้อัตโนมัติ:
- ✅ ตรวจสอบ prerequisites (git, docker)
- 📥 Clone Supabase repository (depth=1 เพื่อความเร็ว)
- 📁 Copy official docker-compose.yml และ config files
- 🔧 ปรับ ports ให้ไม่ชนกับ services อื่น:
- Kong HTTP:
8100(แทน 8000) - PostgreSQL:
5434(แทน 5432) - Pooler:
6544(แทน 6543)
- Kong HTTP:
- 🌐 เพิ่ม
shared_data_networkconfiguration - 🔐 รัน official
generate-keys.shเพื่อสร้าง JWT secrets และ API keys - 📦 Pull Docker images ทั้งหมด
- 🧹 ลบ temporary files
2. Review และแก้ไข .env
หลังจากรัน script แล้ว ต้องแก้ไข .env:
nano .env
สิ่งที่ต้องแก้ไข:
# 1. Database Password (REQUIRED)
POSTGRES_PASSWORD=your-super-secret-postgres-password
# 2. Dashboard Credentials (REQUIRED)
DASHBOARD_USERNAME=admin
DASHBOARD_PASSWORD=your-secure-password-with-letters
# ⚠️ Password ต้องมีตัวอักษรอย่างน้อย 1 ตัว (ห้ามใช้แต่ตัวเลข)
# 3. Public URLs (ถ้าใช้ domain name)
SUPABASE_PUBLIC_URL=http://your-domain:8100
API_EXTERNAL_URL=http://your-domain:8100
SITE_URL=http://your-domain:3000
# หรือใช้ localhost สำหรับ development
SUPABASE_PUBLIC_URL=http://localhost:8100
API_EXTERNAL_URL=http://localhost:8100
SITE_URL=http://localhost:3000
ไม่ต้องแก้ (auto-generated แล้ว):
JWT_SECRET✅ANON_KEY✅SERVICE_ROLE_KEY✅SECRET_KEY_BASE✅VAULT_ENC_KEY✅PG_META_CRYPTO_KEY✅LOGFLARE_PUBLIC_ACCESS_TOKEN✅LOGFLARE_PRIVATE_ACCESS_TOKEN✅S3_PROTOCOL_ACCESS_KEY_ID✅S3_PROTOCOL_ACCESS_KEY_SECRET✅
3. สร้าง Network (ถ้ายังไม่มี)
cd ../00-network
bash create-network.sh
4. Start Supabase
cd ../02-supabase
docker compose up -d
5. ตรวจสอบ Services
# ดู status ทั้งหมด
docker compose ps
# ดู logs
docker compose logs -f
# ดู logs ของ service เฉพาะ
docker compose logs -f studio
docker compose logs -f auth
docker compose logs -f db
รอประมาณ 1-2 นาที จนทุก service มี status Up (healthy)
🌐 Access Points
หลังจาก services ทั้งหมดรันแล้ว:
| Service | URL | Credentials |
|---|---|---|
| Studio Dashboard | http://localhost:8100 | DASHBOARD_USERNAME / DASHBOARD_PASSWORD |
| REST API | http://localhost:8100/rest/v1/ | ANON_KEY or SERVICE_ROLE_KEY |
| Auth API | http://localhost:8100/auth/v1/ | ANON_KEY or SERVICE_ROLE_KEY |
| Storage API | http://localhost:8100/storage/v1/ | ANON_KEY or SERVICE_ROLE_KEY |
| Realtime | http://localhost:8100/realtime/v1/ | ANON_KEY |
| PostgreSQL | localhost:5434 | postgres / POSTGRES_PASSWORD |
| Pooler (Session) | localhost:5434 | postgres.your-tenant-id / POSTGRES_PASSWORD |
| Pooler (Transaction) | localhost:6544 | postgres.your-tenant-id / POSTGRES_PASSWORD |
📝 ไฟล์และ Directories ที่สร้างขึ้น
หลังจากรัน setup2.sh:
02-supabase/
├── docker-compose.yml # Official Supabase compose file
├── .env # Environment variables (with generated secrets)
├── .env.example.new # Backup of official .env.example
├── volumes/ # Configuration and data
│ ├── api/
│ │ └── kong.yml # Kong API Gateway config
│ ├── db/ # Database init scripts
│ │ ├── realtime.sql
│ │ ├── webhooks.sql
│ │ ├── roles.sql
│ │ ├── jwt.sql
│ │ ├── _supabase.sql
│ │ ├── logs.sql
│ │ ├── pooler.sql
│ │ └── data/ # PostgreSQL data (created on first run)
│ ├── functions/ # Edge Functions
│ │ └── main/
│ ├── logs/
│ │ └── vector.yml # Log collection config
│ ├── pooler/
│ │ └── pooler.exs # Connection pooler config
│ ├── storage/ # File storage (created on first run)
│ └── snippets/ # SQL snippets
└── utils/
└── generate-keys.sh # Official key generation script
🔐 API Keys และ Secrets
JWT Secret และ API Keys
Script จะ auto-generate ให้:
- JWT_SECRET: ใช้สำหรับ sign และ verify JWT tokens
- ANON_KEY: Public API key (ใช้ใน client-side)
- Role:
anon - Expires: 5 years
- Role:
- SERVICE_ROLE_KEY: Private API key (ใช้ใน server-side)
- Role:
service_role - Bypass Row Level Security (RLS)
- ห้ามเปิดเผยใน client code!
- Role:
ดู API Keys
# ดู ANON_KEY
grep ANON_KEY .env
# ดู SERVICE_ROLE_KEY
grep SERVICE_ROLE_KEY .env
Verify JWT Tokens
ไปที่ jwt.io แล้ว:
- Paste
ANON_KEYหรือSERVICE_ROLE_KEY - ใส่
JWT_SECRETใน "Verify Signature" section - ตรวจสอบ payload และ expiration
🔄 การอัปเดต Supabase
วิธีที่ 1: รัน setup2.sh ใหม่
cd 02-supabase
# Backup .env ปัจจุบัน
cp .env .env.backup
# รัน setup2.sh ใหม่
bash setup2.sh
# Restore .env settings ที่คุณแก้ไขไว้
# (JWT secrets จะถูก generate ใหม่)
# Restart services
docker compose down
docker compose up -d
วิธีที่ 2: Update แบบ Manual
# Pull latest images
docker compose pull
# Restart services
docker compose down
docker compose up -d
วิธีที่ 3: Update Service เฉพาะ
# 1. ดู version ล่าสุดที่ Docker Hub
# https://hub.docker.com/u/supabase
# 2. แก้ไข docker-compose.yml
# เช่น: image: supabase/studio:2026.02.16-sha-26c615c
# 3. Pull และ restart
docker compose pull studio
docker compose up -d studio
🛠️ Maintenance
Backup Database
# Backup
docker exec supabase-db pg_dump -U postgres postgres > backup_$(date +%Y%m%d).sql
# Restore
docker exec -i supabase-db psql -U postgres postgres < backup_20260218.sql
Backup Storage
# Backup storage files
tar -czf storage_backup_$(date +%Y%m%d).tar.gz volumes/storage/
# Restore
tar -xzf storage_backup_20260218.tar.gz
View Logs
# All services
docker compose logs -f
# Specific service
docker compose logs -f studio
docker compose logs -f auth
docker compose logs -f db
docker compose logs -f kong
Restart Services
# Restart all
docker compose restart
# Restart specific service
docker compose restart studio
docker compose restart auth
🐛 Troubleshooting
Services ไม่ start
# ดู logs
docker compose logs
# ตรวจสอบ network
docker network inspect shared_data_network
# ตรวจสอบ .env
cat .env | grep -v '^#' | grep -v '^$'
Database connection error
# ตรวจสอบ database พร้อมหรือยัง
docker exec supabase-db pg_isready -U postgres
# ดู database logs
docker compose logs db
Port conflicts
ถ้า port ชน แก้ไขใน .env:
KONG_HTTP_PORT=8101
POSTGRES_PORT=5435
POOLER_PROXY_PORT_TRANSACTION=6545
แล้วแก้ใน docker-compose.yml ตรง ports mapping ด้วย
"container supabase-vector exited (0)"
ถ้าใช้ rootless Docker ให้แก้ไข .env:
DOCKER_SOCKET_LOCATION=/run/user/1000/docker.sock
🔒 Security Best Practices
-
เปลี่ยน default passwords ทั้งหมด
POSTGRES_PASSWORDDASHBOARD_PASSWORD
-
ใช้ HTTPS ใน production
- Setup Nginx Proxy Manager
- ติดตั้ง SSL certificate
-
Enable Row Level Security (RLS)
- สำหรับทุก table ที่มี sensitive data
-
เก็บ SERVICE_ROLE_KEY ปลอดภัย
- ใช้เฉพาะ server-side
- ห้ามเปิดเผยใน client code
-
Backup เป็นประจำ
- Database
- Storage files
- .env file
-
Monitor logs
- ตรวจสอบ suspicious activities
- ใช้ Logflare analytics
-
Update เป็นประจำ
- ติดตาม Supabase Changelog
- Update images สำหรับ security patches
🗑️ Uninstall
⚠️ คำเตือน: จะลบข้อมูลทั้งหมด!
# Stop และลบ containers + volumes
docker compose down -v
# ลบ database data
rm -rf volumes/db/data
# ลบ storage data
rm -rf volumes/storage
# ลบทุกอย่าง (optional)
cd ..
rm -rf 02-supabase
📚 เอกสารเพิ่มเติม
- Official Self-Hosting Guide
- Supabase Docker Changelog
- Supabase GitHub
- Docker Hub - Supabase Images
- PostgREST Documentation
- GoTrue (Auth) Documentation
💡 Tips
-
ใช้ generate-keys.sh ใหม่เมื่อต้องการ
bash utils/generate-keys.sh -
Test API ด้วย curl
# Get ANON_KEY from .env ANON_KEY=$(grep ANON_KEY .env | cut -d '=' -f2) # Test REST API curl http://localhost:8100/rest/v1/ \ -H "apikey: $ANON_KEY" \ -H "Authorization: Bearer $ANON_KEY" -
Connect จาก client application
import { createClient } from '@supabase/supabase-js' const supabase = createClient( 'http://localhost:8100', 'YOUR_ANON_KEY' ) -
ใช้ Supabase CLI สำหรับ development
npm install -g supabase supabase link --project-ref your-project
🆚 เมื่อไหร่ควรใช้ setup2.sh
ใช้ setup2.sh เมื่อ:
- ✅ ต้องการ official configuration ล่าสุด
- ✅ ต้องการ update ง่ายตาม official releases
- ✅ ต้องการใช้ official tools (generate-keys.sh)
- ✅ Production deployment
- ✅ ต้องการ official support
ใช้ setup.sh เมื่อ:
- ✅ ต้องการ quick setup
- ✅ ต้องการ custom configuration
- ✅ ต้องการ minimal setup
- ✅ Development/testing เท่านั้น
📞 Support
หากมีปัญหา:
- ตรวจสอบ logs:
docker compose logs - อ่าน Troubleshooting Guide
- ดู GitHub Discussions
- ตรวจสอบ GitHub Issues