Files
websitebuilder/DATABASE_SETUP.md
Kunthawat Greethong 4d1bb6892b
Some checks failed
CI / build (push) Has been cancelled
CI / test (push) Has been cancelled
CI / e2e (push) Has been cancelled
CI / lint (push) Has been cancelled
Add websitebuilder app
2026-01-26 12:50:12 +07:00

147 lines
2.4 KiB
Markdown

# Database Setup
## Quick Start with Docker
The easiest way to set up the development database is using Docker Compose.
### Prerequisites
- Docker installed on your machine
- Docker Compose installed
### Start the Database Services
```bash
docker-compose up -d
```
This will start:
- PostgreSQL 16 on port 5432
- Redis 7 on port 6379
### Stop the Database Services
```bash
docker-compose down
```
### View Logs
```bash
docker-compose logs -f
```
### Reset the Database
```bash
docker-compose down -v
docker-compose up -d
```
## Manual PostgreSQL Setup
If you prefer to install PostgreSQL locally:
### Install PostgreSQL
**macOS (Homebrew):**
```bash
brew install postgresql@16
brew services start postgresql@16
```
**Ubuntu/Debian:**
```bash
sudo apt-get update
sudo apt-get install postgresql-16
```
**Windows:**
Download and install from [PostgreSQL Official Site](https://www.postgresql.org/download/windows/)
### Create Database
```bash
# Connect to PostgreSQL
psql -U postgres
# Create database and user
CREATE DATABASE moreminimore;
CREATE USER moreminimore WITH PASSWORD 'moreminimore_password';
GRANT ALL PRIVILEGES ON DATABASE moreminimore TO moreminimore;
\q
```
### Update .env.local
Create a `.env.local` file in the project root:
```env
DATABASE_URL=postgresql://moreminimore:moreminimore_password@localhost:5432/moreminimore
REDIS_URL=redis://localhost:6379
```
## Verify Connection
Run the following command to verify the database connection:
```bash
psql postgresql://moreminimore:moreminimore_password@localhost:5432/moreminimore -c "SELECT version();"
```
You should see the PostgreSQL version information.
## Database Migrations
After setting up the database, run migrations:
```bash
npm run db:generate
npm run db:migrate
```
## Troubleshooting
### Port Already in Use
If port 5432 is already in use, you can change the port in `docker-compose.yml`:
```yaml
ports:
- '5433:5432' # Use 5433 instead
```
Then update your `.env.local`:
```env
DATABASE_URL=postgresql://moreminimore:moreminimore_password@localhost:5433/moreminimore
```
### Connection Refused
Make sure the PostgreSQL service is running:
```bash
docker-compose ps
```
If it's not running, start it:
```bash
docker-compose up -d
```
### Reset Database
To completely reset the database:
```bash
docker-compose down -v
docker-compose up -d
```
This will delete all data and recreate the database from scratch.