chore: prepare portable postgres development environment
This commit is contained in:
@@ -1,5 +1,9 @@
|
|||||||
NODE_ENV=development
|
NODE_ENV=development
|
||||||
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/personal_brand
|
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
|
API_PORT=3001
|
||||||
ADMIN_PORT=3000
|
ADMIN_PORT=3000
|
||||||
SESSION_SECRET=replace-with-a-long-random-value
|
SESSION_SECRET=replace-with-a-long-random-value
|
||||||
|
|||||||
38
README.md
38
README.md
@@ -28,3 +28,41 @@ corepack enable
|
|||||||
pnpm install
|
pnpm install
|
||||||
pnpm check
|
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`。
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
"start": "node dist/main.js",
|
"start": "node dist/main.js",
|
||||||
"db:generate": "prisma generate",
|
"db:generate": "prisma generate",
|
||||||
"db:migrate": "prisma migrate dev",
|
"db:migrate": "prisma migrate dev",
|
||||||
|
"db:migrate:deploy": "prisma migrate deploy",
|
||||||
"db:seed": "tsx prisma/seed.ts"
|
"db:seed": "tsx prisma/seed.ts"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@@ -1,15 +1,22 @@
|
|||||||
services:
|
services:
|
||||||
postgres:
|
postgres:
|
||||||
image: postgres:16-alpine
|
image: postgres:16-alpine
|
||||||
|
container_name: personal-brand-postgres
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_USER: postgres
|
POSTGRES_USER: ${POSTGRES_USER:-postgres}
|
||||||
POSTGRES_PASSWORD: postgres
|
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
|
||||||
POSTGRES_DB: personal_brand
|
POSTGRES_DB: ${POSTGRES_DB:-personal_brand}
|
||||||
ports:
|
ports:
|
||||||
- "5432:5432"
|
- "${POSTGRES_PORT:-5432}:5432"
|
||||||
volumes:
|
volumes:
|
||||||
- personal-brand-postgres:/var/lib/postgresql/data
|
- 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:
|
volumes:
|
||||||
personal-brand-postgres:
|
personal-brand-postgres:
|
||||||
|
|||||||
Reference in New Issue
Block a user