chore: prepare portable postgres development environment

This commit is contained in:
2026-09-18 17:30:42 +08:00
parent 0e8b8e1591
commit f2687c502a
4 changed files with 54 additions and 4 deletions

View File

@@ -1,5 +1,9 @@
NODE_ENV=development
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/personal_brand
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=personal_brand
POSTGRES_PORT=5432
API_PORT=3001
ADMIN_PORT=3000
SESSION_SECRET=replace-with-a-long-random-value

View File

@@ -28,3 +28,41 @@ corepack enable
pnpm install
pnpm check
```
## 本地数据库
在有 Docker 的机器上执行:
```bash
cp .env.example .env
docker compose up -d postgres
docker compose ps
pnpm db:migrate
pnpm db:seed
```
确认数据库健康后,再启动 API
```bash
pnpm --filter @personal-brand/api dev
```
停止数据库但保留数据:
```bash
docker compose stop postgres
```
停止并删除容器但保留数据卷:
```bash
docker compose down
```
删除容器和本地数据库数据(不可恢复):
```bash
docker compose down -v
```
`DATABASE_URL` 用于宿主机运行 API 时连接 `localhost`。如果以后把 API 也放进 Compose连接地址应改为服务名 `postgres`,不能继续使用 `localhost`

View File

@@ -11,6 +11,7 @@
"start": "node dist/main.js",
"db:generate": "prisma generate",
"db:migrate": "prisma migrate dev",
"db:migrate:deploy": "prisma migrate deploy",
"db:seed": "tsx prisma/seed.ts"
},
"dependencies": {

View File

@@ -1,15 +1,22 @@
services:
postgres:
image: postgres:16-alpine
container_name: personal-brand-postgres
restart: unless-stopped
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: personal_brand
POSTGRES_USER: ${POSTGRES_USER:-postgres}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
POSTGRES_DB: ${POSTGRES_DB:-personal_brand}
ports:
- "5432:5432"
- "${POSTGRES_PORT:-5432}:5432"
volumes:
- personal-brand-postgres:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
interval: 5s
timeout: 5s
retries: 12
start_period: 5s
volumes:
personal-brand-postgres: