🎨 重构用户端前端为vue开发,完善基础类和角色相关接口
This commit is contained in:
14
deploy/.gitignore
vendored
Normal file
14
deploy/.gitignore
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
# PostgreSQL 数据
|
||||
postgres/data/
|
||||
*.sql.backup
|
||||
*.dump
|
||||
|
||||
# 日志文件
|
||||
*.log
|
||||
|
||||
# 环境变量文件(如果创建的话)
|
||||
.env
|
||||
.env.local
|
||||
|
||||
# pgAdmin 数据
|
||||
pgadmin_data/
|
||||
293
deploy/README.postgres.md
Normal file
293
deploy/README.postgres.md
Normal file
@@ -0,0 +1,293 @@
|
||||
# PostgreSQL Docker 部署指南
|
||||
|
||||
## 📋 目录结构
|
||||
|
||||
```
|
||||
deploy/
|
||||
├── docker-compose.postgres.yml # Docker Compose 配置
|
||||
├── postgres/
|
||||
│ ├── init.sql # 数据库初始化脚本
|
||||
│ └── postgresql.conf # PostgreSQL 配置文件
|
||||
└── README.postgres.md # 本文档
|
||||
```
|
||||
|
||||
## 🚀 快速启动
|
||||
|
||||
### 1. 启动数据库
|
||||
|
||||
```bash
|
||||
# 在 deploy 目录下执行
|
||||
cd deploy
|
||||
docker-compose -f docker-compose.postgres.yml up -d
|
||||
```
|
||||
|
||||
### 2. 查看日志
|
||||
|
||||
```bash
|
||||
# 查看 PostgreSQL 日志
|
||||
docker-compose -f docker-compose.postgres.yml logs -f postgres
|
||||
|
||||
# 查看所有服务日志
|
||||
docker-compose -f docker-compose.postgres.yml logs -f
|
||||
```
|
||||
|
||||
### 3. 停止服务
|
||||
|
||||
```bash
|
||||
# 停止服务(保留数据)
|
||||
docker-compose -f docker-compose.postgres.yml stop
|
||||
|
||||
# 停止并删除容器(保留数据卷)
|
||||
docker-compose -f docker-compose.postgres.yml down
|
||||
|
||||
# 停止并删除所有内容(包括数据)
|
||||
docker-compose -f docker-compose.postgres.yml down -v
|
||||
```
|
||||
|
||||
## 🔧 配置说明
|
||||
|
||||
### 数据库连接信息
|
||||
|
||||
| 项目 | 值 |
|
||||
|------|-----|
|
||||
| Host | localhost |
|
||||
| Port | 5432 |
|
||||
| Database | st_dev |
|
||||
| Username | st_user |
|
||||
| Password | st_password |
|
||||
|
||||
### pgAdmin 管理界面
|
||||
|
||||
- **访问地址**: http://localhost:5050
|
||||
- **登录邮箱**: admin@st.local
|
||||
- **登录密码**: admin123
|
||||
|
||||
### 已安装的扩展
|
||||
|
||||
- ✅ **pgvector** - 向量相似度搜索(用于 AI 记忆功能)
|
||||
- ✅ **uuid-ossp** - UUID 生成
|
||||
- ✅ **pg_trgm** - 模糊搜索和全文搜索
|
||||
- ✅ **btree_gin** - GIN 索引优化(加速 JSONB 查询)
|
||||
|
||||
## 📝 更新 Go 配置
|
||||
|
||||
修改 `server/config.yaml` 中的数据库配置:
|
||||
|
||||
```yaml
|
||||
# system/config.yaml
|
||||
system:
|
||||
db-type: postgres
|
||||
|
||||
pgsql:
|
||||
path: 127.0.0.1
|
||||
port: "5432"
|
||||
config: sslmode=disable TimeZone=Asia/Shanghai
|
||||
db-name: st_dev
|
||||
username: st_user
|
||||
password: st_password
|
||||
max-idle-conns: 10
|
||||
max-open-conns: 100
|
||||
log-mode: info
|
||||
log-zap: false
|
||||
```
|
||||
|
||||
## 🔍 数据库管理
|
||||
|
||||
### 使用 Docker 命令连接
|
||||
|
||||
```bash
|
||||
# 进入 PostgreSQL 容器
|
||||
docker exec -it st-postgres bash
|
||||
|
||||
# 连接数据库
|
||||
psql -U st_user -d st_dev
|
||||
|
||||
# 或者直接执行
|
||||
docker exec -it st-postgres psql -U st_user -d st_dev
|
||||
```
|
||||
|
||||
### 常用 SQL 命令
|
||||
|
||||
```sql
|
||||
-- 查看已安装的扩展
|
||||
\dx
|
||||
|
||||
-- 查看所有表
|
||||
\dt
|
||||
|
||||
-- 查看表结构
|
||||
\d+ table_name
|
||||
|
||||
-- 查看数据库大小
|
||||
SELECT pg_size_pretty(pg_database_size('st_dev'));
|
||||
|
||||
-- 查看表大小
|
||||
SELECT
|
||||
schemaname,
|
||||
tablename,
|
||||
pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename)) AS size
|
||||
FROM pg_tables
|
||||
WHERE schemaname = 'public'
|
||||
ORDER BY pg_total_relation_size(schemaname||'.'||tablename) DESC;
|
||||
|
||||
-- 测试 pgvector 扩展
|
||||
SELECT vector_dims(ARRAY[1,2,3]::vector);
|
||||
|
||||
-- 查看当前连接数
|
||||
SELECT count(*) FROM pg_stat_activity;
|
||||
```
|
||||
|
||||
## 🔄 数据备份与恢复
|
||||
|
||||
### 备份数据库
|
||||
|
||||
```bash
|
||||
# 备份整个数据库
|
||||
docker exec st-postgres pg_dump -U st_user st_dev > backup_$(date +%Y%m%d_%H%M%S).sql
|
||||
|
||||
# 备份为自定义格式(推荐,支持并行恢复)
|
||||
docker exec st-postgres pg_dump -U st_user -Fc st_dev > backup_$(date +%Y%m%d_%H%M%S).dump
|
||||
```
|
||||
|
||||
### 恢复数据库
|
||||
|
||||
```bash
|
||||
# 从 SQL 文件恢复
|
||||
docker exec -i st-postgres psql -U st_user -d st_dev < backup.sql
|
||||
|
||||
# 从自定义格式恢复
|
||||
docker exec -i st-postgres pg_restore -U st_user -d st_dev -c backup.dump
|
||||
```
|
||||
|
||||
## 🛠️ 性能优化建议
|
||||
|
||||
### 开发环境(当前配置)
|
||||
|
||||
- `shared_buffers`: 256MB
|
||||
- `effective_cache_size`: 1GB
|
||||
- `work_mem`: 5MB
|
||||
|
||||
### 生产环境(8GB 内存服务器)
|
||||
|
||||
修改 `postgresql.conf`:
|
||||
|
||||
```conf
|
||||
shared_buffers = 2GB # 系统内存的 25%
|
||||
effective_cache_size = 6GB # 系统内存的 75%
|
||||
maintenance_work_mem = 512MB
|
||||
work_mem = 16MB
|
||||
```
|
||||
|
||||
## 🐛 常见问题
|
||||
|
||||
### 1. 端口被占用
|
||||
|
||||
如果 5432 端口被占用,修改 `docker-compose.postgres.yml`:
|
||||
|
||||
```yaml
|
||||
ports:
|
||||
- "15432:5432" # 映射到本地 15432 端口
|
||||
```
|
||||
|
||||
### 2. 权限问题
|
||||
|
||||
```bash
|
||||
# Linux/Mac 系统可能需要修改数据目录权限
|
||||
sudo chown -R 999:999 /path/to/postgres_data
|
||||
```
|
||||
|
||||
### 3. 连接失败
|
||||
|
||||
检查防火墙和 Docker 网络:
|
||||
|
||||
```bash
|
||||
# 查看容器状态
|
||||
docker ps
|
||||
|
||||
# 查看容器日志
|
||||
docker logs st-postgres
|
||||
|
||||
# 测试连接
|
||||
docker exec st-postgres pg_isready -U st_user
|
||||
```
|
||||
|
||||
### 4. 性能调优
|
||||
|
||||
```bash
|
||||
# 查看当前配置
|
||||
docker exec st-postgres psql -U st_user -d st_dev -c "SHOW ALL;"
|
||||
|
||||
# 分析慢查询
|
||||
docker exec st-postgres psql -U st_user -d st_dev -c "
|
||||
SELECT query, calls, total_time, mean_time
|
||||
FROM pg_stat_statements
|
||||
ORDER BY mean_time DESC
|
||||
LIMIT 10;
|
||||
"
|
||||
```
|
||||
|
||||
## 📊 监控
|
||||
|
||||
### 使用 pgAdmin
|
||||
|
||||
1. 访问 http://localhost:5050
|
||||
2. 登录后,添加新服务器:
|
||||
- Name: ST Development
|
||||
- Host: postgres (容器名称)
|
||||
- Port: 5432
|
||||
- Username: st_user
|
||||
- Password: st_password
|
||||
|
||||
### 使用命令行监控
|
||||
|
||||
```bash
|
||||
# 查看活动连接
|
||||
docker exec st-postgres psql -U st_user -d st_dev -c "
|
||||
SELECT pid, usename, application_name, client_addr, state, query
|
||||
FROM pg_stat_activity
|
||||
WHERE datname = 'st_dev';
|
||||
"
|
||||
|
||||
# 查看表统计信息
|
||||
docker exec st-postgres psql -U st_user -d st_dev -c "
|
||||
SELECT schemaname, tablename, n_live_tup, n_dead_tup
|
||||
FROM pg_stat_user_tables
|
||||
ORDER BY n_live_tup DESC;
|
||||
"
|
||||
```
|
||||
|
||||
## 🔐 安全建议
|
||||
|
||||
### 生产环境部署
|
||||
|
||||
1. **修改默认密码**:
|
||||
```yaml
|
||||
environment:
|
||||
POSTGRES_PASSWORD: 使用强密码
|
||||
```
|
||||
|
||||
2. **限制网络访问**:
|
||||
```yaml
|
||||
ports:
|
||||
- "127.0.0.1:5432:5432" # 只允许本地访问
|
||||
```
|
||||
|
||||
3. **启用 SSL**:
|
||||
```conf
|
||||
ssl = on
|
||||
ssl_cert_file = '/path/to/cert.pem'
|
||||
ssl_key_file = '/path/to/key.pem'
|
||||
```
|
||||
|
||||
4. **定期备份**:
|
||||
```bash
|
||||
# 添加到 crontab
|
||||
0 2 * * * docker exec st-postgres pg_dump -U st_user -Fc st_dev > /backups/st_$(date +\%Y\%m\%d).dump
|
||||
```
|
||||
|
||||
## 📚 参考资料
|
||||
|
||||
- [PostgreSQL 官方文档](https://www.postgresql.org/docs/18/)
|
||||
- [pgvector 扩展文档](https://github.com/pgvector/pgvector)
|
||||
- [Docker Hub - PostgreSQL](https://hub.docker.com/_/postgres)
|
||||
- [pgAdmin 文档](https://www.pgadmin.org/docs/)
|
||||
86
deploy/docker-compose.postgres-official.yml
Normal file
86
deploy/docker-compose.postgres-official.yml
Normal file
@@ -0,0 +1,86 @@
|
||||
version: '3.8'
|
||||
|
||||
services:
|
||||
postgres:
|
||||
# 使用自定义 Dockerfile 构建(基于官方镜像)
|
||||
build:
|
||||
context: ./postgres
|
||||
dockerfile: Dockerfile
|
||||
image: st-postgres:18.1-pgvector
|
||||
container_name: st-postgres
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- "5432:5432"
|
||||
environment:
|
||||
# 数据库配置
|
||||
POSTGRES_DB: st_dev
|
||||
POSTGRES_USER: st_user
|
||||
POSTGRES_PASSWORD: st_password
|
||||
# PostgreSQL 配置
|
||||
POSTGRES_INITDB_ARGS: "--encoding=UTF8 --locale=C"
|
||||
# 时区设置
|
||||
TZ: Asia/Shanghai
|
||||
PGTZ: Asia/Shanghai
|
||||
volumes:
|
||||
# 数据持久化
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
# 初始化脚本
|
||||
- ./postgres/init.sql:/docker-entrypoint-initdb.d/01-init.sql:ro
|
||||
# PostgreSQL 配置文件(可选)
|
||||
- ./postgres/postgresql.conf:/etc/postgresql/postgresql.conf:ro
|
||||
command: >
|
||||
postgres
|
||||
-c shared_preload_libraries=vector
|
||||
-c max_connections=200
|
||||
-c shared_buffers=256MB
|
||||
-c effective_cache_size=1GB
|
||||
-c maintenance_work_mem=128MB
|
||||
-c checkpoint_completion_target=0.9
|
||||
-c wal_buffers=16MB
|
||||
-c default_statistics_target=100
|
||||
-c random_page_cost=1.1
|
||||
-c effective_io_concurrency=200
|
||||
-c work_mem=5242kB
|
||||
-c huge_pages=off
|
||||
-c min_wal_size=1GB
|
||||
-c max_wal_size=4GB
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U st_user -d st_dev"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
networks:
|
||||
- st-network
|
||||
|
||||
# pgAdmin - PostgreSQL Web 管理界面(可选)
|
||||
pgadmin:
|
||||
image: dpage/pgadmin4:latest
|
||||
container_name: st-pgadmin
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- "5050:80"
|
||||
environment:
|
||||
PGADMIN_DEFAULT_EMAIL: admin@st.local
|
||||
PGADMIN_DEFAULT_PASSWORD: admin123
|
||||
PGADMIN_CONFIG_SERVER_MODE: 'False'
|
||||
PGADMIN_CONFIG_MASTER_PASSWORD_REQUIRED: 'False'
|
||||
volumes:
|
||||
- pgadmin_data:/var/lib/pgadmin
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
networks:
|
||||
- st-network
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
driver: local
|
||||
name: st-postgres-data
|
||||
pgadmin_data:
|
||||
driver: local
|
||||
name: st-pgadmin-data
|
||||
|
||||
networks:
|
||||
st-network:
|
||||
driver: bridge
|
||||
name: st-network
|
||||
82
deploy/docker-compose.postgres.yml
Normal file
82
deploy/docker-compose.postgres.yml
Normal file
@@ -0,0 +1,82 @@
|
||||
version: '3.8'
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: ankane/pgvector:v0.7.4-pg18
|
||||
container_name: st-postgres
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- "5432:5432"
|
||||
environment:
|
||||
# 数据库配置
|
||||
POSTGRES_DB: st_dev
|
||||
POSTGRES_USER: st_user
|
||||
POSTGRES_PASSWORD: st_password
|
||||
# PostgreSQL 配置
|
||||
POSTGRES_INITDB_ARGS: "--encoding=UTF8 --locale=C"
|
||||
# 时区设置
|
||||
TZ: Asia/Shanghai
|
||||
PGTZ: Asia/Shanghai
|
||||
volumes:
|
||||
# 数据持久化
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
# 初始化脚本
|
||||
- ./postgres/init.sql:/docker-entrypoint-initdb.d/01-init.sql:ro
|
||||
# PostgreSQL 配置文件(可选)
|
||||
- ./postgres/postgresql.conf:/etc/postgresql/postgresql.conf:ro
|
||||
command: >
|
||||
postgres
|
||||
-c shared_preload_libraries=vector
|
||||
-c max_connections=200
|
||||
-c shared_buffers=256MB
|
||||
-c effective_cache_size=1GB
|
||||
-c maintenance_work_mem=128MB
|
||||
-c checkpoint_completion_target=0.9
|
||||
-c wal_buffers=16MB
|
||||
-c default_statistics_target=100
|
||||
-c random_page_cost=1.1
|
||||
-c effective_io_concurrency=200
|
||||
-c work_mem=5242kB
|
||||
-c huge_pages=off
|
||||
-c min_wal_size=1GB
|
||||
-c max_wal_size=4GB
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U st_user -d st_dev"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
networks:
|
||||
- st-network
|
||||
|
||||
# pgAdmin - PostgreSQL Web 管理界面(可选)
|
||||
pgadmin:
|
||||
image: dpage/pgadmin4:latest
|
||||
container_name: st-pgadmin
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- "5050:80"
|
||||
environment:
|
||||
PGADMIN_DEFAULT_EMAIL: admin@st.local
|
||||
PGADMIN_DEFAULT_PASSWORD: admin123
|
||||
PGADMIN_CONFIG_SERVER_MODE: 'False'
|
||||
PGADMIN_CONFIG_MASTER_PASSWORD_REQUIRED: 'False'
|
||||
volumes:
|
||||
- pgadmin_data:/var/lib/pgadmin
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
networks:
|
||||
- st-network
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
driver: local
|
||||
name: st-postgres-data
|
||||
pgadmin_data:
|
||||
driver: local
|
||||
name: st-pgadmin-data
|
||||
|
||||
networks:
|
||||
st-network:
|
||||
driver: bridge
|
||||
name: st-network
|
||||
42
deploy/postgres/Dockerfile
Normal file
42
deploy/postgres/Dockerfile
Normal file
@@ -0,0 +1,42 @@
|
||||
# ====================================================
|
||||
# 基于官方 PostgreSQL 18.1 镜像 + pgvector 扩展
|
||||
# ====================================================
|
||||
|
||||
FROM postgres:18.1
|
||||
|
||||
# 设置维护者信息
|
||||
LABEL maintainer="st-dev@example.com"
|
||||
LABEL description="PostgreSQL 18.1 with pgvector extension"
|
||||
|
||||
# 安装依赖
|
||||
RUN apt-get update && \
|
||||
apt-get install -y --no-install-recommends \
|
||||
build-essential \
|
||||
git \
|
||||
postgresql-server-dev-18 \
|
||||
ca-certificates && \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# 安装 pgvector 扩展
|
||||
RUN cd /tmp && \
|
||||
git clone --branch v0.7.4 https://github.com/pgvector/pgvector.git && \
|
||||
cd pgvector && \
|
||||
make && \
|
||||
make install && \
|
||||
cd / && \
|
||||
rm -rf /tmp/pgvector
|
||||
|
||||
# 清理
|
||||
RUN apt-get purge -y --auto-remove build-essential git postgresql-server-dev-18 && \
|
||||
apt-get clean && \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# 设置时区
|
||||
ENV TZ=Asia/Shanghai
|
||||
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
|
||||
|
||||
# 暴露端口
|
||||
EXPOSE 5432
|
||||
|
||||
# 使用默认的 PostgreSQL 入口点
|
||||
CMD ["postgres"]
|
||||
82
deploy/postgres/init.sql
Normal file
82
deploy/postgres/init.sql
Normal file
@@ -0,0 +1,82 @@
|
||||
-- ====================================================
|
||||
-- 云酒馆数据库初始化脚本
|
||||
-- ====================================================
|
||||
|
||||
-- 设置客户端编码
|
||||
SET client_encoding = 'UTF8';
|
||||
|
||||
-- 创建必要的扩展
|
||||
-- ====================================================
|
||||
|
||||
-- 1. pgvector - 向量相似度搜索扩展(用于 AI 记忆功能)
|
||||
CREATE EXTENSION IF NOT EXISTS vector;
|
||||
|
||||
-- 2. uuid-ossp - UUID 生成扩展(用于生成唯一标识符)
|
||||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
||||
|
||||
-- 3. pg_trgm - 三元组模糊匹配扩展(用于全文搜索)
|
||||
CREATE EXTENSION IF NOT EXISTS pg_trgm;
|
||||
|
||||
-- 4. btree_gin - GIN 索引扩展(用于加速 JSONB 查询)
|
||||
CREATE EXTENSION IF NOT EXISTS btree_gin;
|
||||
|
||||
-- 验证扩展是否安装成功
|
||||
-- ====================================================
|
||||
DO $$
|
||||
BEGIN
|
||||
RAISE NOTICE '===========================================';
|
||||
RAISE NOTICE '云酒馆数据库初始化完成!';
|
||||
RAISE NOTICE '===========================================';
|
||||
RAISE NOTICE '已安装的扩展:';
|
||||
RAISE NOTICE ' ✓ vector - 向量相似度搜索';
|
||||
RAISE NOTICE ' ✓ uuid-ossp - UUID 生成';
|
||||
RAISE NOTICE ' ✓ pg_trgm - 模糊搜索';
|
||||
RAISE NOTICE ' ✓ btree_gin - GIN 索引优化';
|
||||
RAISE NOTICE '===========================================';
|
||||
RAISE NOTICE '数据库信息:';
|
||||
RAISE NOTICE ' 数据库名: st_dev';
|
||||
RAISE NOTICE ' 用户名: st_user';
|
||||
RAISE NOTICE ' 字符集: UTF8';
|
||||
RAISE NOTICE '===========================================';
|
||||
END $$;
|
||||
|
||||
-- 创建用户并授予权限
|
||||
-- ====================================================
|
||||
-- 如果需要额外的只读用户,可以取消以下注释
|
||||
-- CREATE USER st_readonly WITH PASSWORD 'readonly_password';
|
||||
-- GRANT CONNECT ON DATABASE st_dev TO st_readonly;
|
||||
-- GRANT USAGE ON SCHEMA public TO st_readonly;
|
||||
-- GRANT SELECT ON ALL TABLES IN SCHEMA public TO st_readonly;
|
||||
-- ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO st_readonly;
|
||||
|
||||
-- 性能优化设置
|
||||
-- ====================================================
|
||||
-- 启用 JIT 编译(PostgreSQL 11+)
|
||||
SET jit = on;
|
||||
|
||||
-- 设置默认的统计信息采样
|
||||
ALTER DATABASE st_dev SET default_statistics_target = 100;
|
||||
|
||||
-- 优化大数据量查询
|
||||
ALTER DATABASE st_dev SET work_mem = '16MB';
|
||||
|
||||
-- 提示信息
|
||||
-- ====================================================
|
||||
\echo ''
|
||||
\echo '=========================================='
|
||||
\echo '数据库初始化完成!'
|
||||
\echo '=========================================='
|
||||
\echo '连接信息:'
|
||||
\echo ' Host: localhost'
|
||||
\echo ' Port: 5432'
|
||||
\echo ' Database: st_dev'
|
||||
\echo ' Username: st_user'
|
||||
\echo ' Password: st_password'
|
||||
\echo '=========================================='
|
||||
\echo ''
|
||||
\echo '管理工具:'
|
||||
\echo ' pgAdmin: http://localhost:5050'
|
||||
\echo ' Email: admin@st.local'
|
||||
\echo ' Pass: admin123'
|
||||
\echo '=========================================='
|
||||
\echo ''
|
||||
79
deploy/postgres/postgresql.conf
Normal file
79
deploy/postgres/postgresql.conf
Normal file
@@ -0,0 +1,79 @@
|
||||
# ====================================================
|
||||
# PostgreSQL 配置文件(针对开发环境优化)
|
||||
# ====================================================
|
||||
|
||||
# 连接设置
|
||||
# ====================================================
|
||||
listen_addresses = '*'
|
||||
max_connections = 200
|
||||
superuser_reserved_connections = 3
|
||||
|
||||
# 内存设置
|
||||
# ====================================================
|
||||
shared_buffers = 256MB # 共享缓冲区(推荐系统内存的 25%)
|
||||
effective_cache_size = 1GB # 有效缓存大小(推荐系统内存的 50-75%)
|
||||
maintenance_work_mem = 128MB # 维护操作内存
|
||||
work_mem = 5MB # 排序和哈希表内存
|
||||
|
||||
# WAL(Write-Ahead Logging)设置
|
||||
# ====================================================
|
||||
wal_buffers = 16MB
|
||||
min_wal_size = 1GB
|
||||
max_wal_size = 4GB
|
||||
checkpoint_completion_target = 0.9
|
||||
|
||||
# 查询优化
|
||||
# ====================================================
|
||||
random_page_cost = 1.1 # SSD 优化(默认 4.0)
|
||||
effective_io_concurrency = 200 # SSD 优化(默认 1)
|
||||
default_statistics_target = 100
|
||||
|
||||
# 日志设置
|
||||
# ====================================================
|
||||
logging_collector = on
|
||||
log_destination = 'stderr'
|
||||
log_directory = 'log'
|
||||
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
|
||||
log_rotation_age = 1d
|
||||
log_rotation_size = 100MB
|
||||
log_line_prefix = '%m [%p] %u@%d '
|
||||
log_timezone = 'Asia/Shanghai'
|
||||
|
||||
# 慢查询日志
|
||||
log_min_duration_statement = 1000 # 记录超过 1 秒的查询
|
||||
log_statement = 'ddl' # 记录 DDL 语句
|
||||
|
||||
# 扩展配置
|
||||
# ====================================================
|
||||
shared_preload_libraries = 'vector' # 预加载 pgvector 扩展
|
||||
|
||||
# JIT 编译(提升性能)
|
||||
# ====================================================
|
||||
jit = on
|
||||
jit_above_cost = 100000
|
||||
jit_inline_above_cost = 500000
|
||||
jit_optimize_above_cost = 500000
|
||||
|
||||
# 时区设置
|
||||
# ====================================================
|
||||
timezone = 'Asia/Shanghai'
|
||||
|
||||
# 客户端连接默认设置
|
||||
# ====================================================
|
||||
client_encoding = 'UTF8'
|
||||
lc_messages = 'C'
|
||||
lc_monetary = 'C'
|
||||
lc_numeric = 'C'
|
||||
lc_time = 'C'
|
||||
|
||||
# 并行查询设置(提升大数据量查询性能)
|
||||
# ====================================================
|
||||
max_parallel_workers_per_gather = 2
|
||||
max_parallel_workers = 4
|
||||
max_worker_processes = 4
|
||||
|
||||
# 自动清理设置
|
||||
# ====================================================
|
||||
autovacuum = on
|
||||
autovacuum_max_workers = 3
|
||||
autovacuum_naptime = 1min
|
||||
109
deploy/start-postgres.sh
Normal file
109
deploy/start-postgres.sh
Normal file
@@ -0,0 +1,109 @@
|
||||
#!/bin/bash
|
||||
|
||||
# ====================================================
|
||||
# PostgreSQL Docker 快速启动脚本
|
||||
# ====================================================
|
||||
|
||||
set -e
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
echo "=========================================="
|
||||
echo "云酒馆 PostgreSQL 启动脚本"
|
||||
echo "=========================================="
|
||||
echo ""
|
||||
|
||||
# 检查 Docker 是否安装
|
||||
if ! command -v docker &> /dev/null; then
|
||||
echo "❌ 错误: 未检测到 Docker,请先安装 Docker"
|
||||
echo " 安装地址: https://www.docker.com/get-started"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 检查 Docker Compose 是否安装
|
||||
if ! command -v docker-compose &> /dev/null; then
|
||||
echo "❌ 错误: 未检测到 Docker Compose,请先安装"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 检查 Docker 是否运行
|
||||
if ! docker info &> /dev/null; then
|
||||
echo "❌ 错误: Docker 服务未运行,请先启动 Docker"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "✅ Docker 环境检查通过"
|
||||
echo ""
|
||||
|
||||
# 创建必要的目录
|
||||
mkdir -p postgres
|
||||
|
||||
# 检查容器是否已经运行
|
||||
if docker ps -a --format '{{.Names}}' | grep -q '^st-postgres$'; then
|
||||
echo "📦 检测到已存在的容器"
|
||||
|
||||
if docker ps --format '{{.Names}}' | grep -q '^st-postgres$'; then
|
||||
echo " 容器正在运行中..."
|
||||
echo ""
|
||||
echo "如需重启,请先执行: $0 stop"
|
||||
exit 0
|
||||
else
|
||||
echo " 正在启动已存在的容器..."
|
||||
docker-compose -f docker-compose.postgres.yml start
|
||||
fi
|
||||
else
|
||||
echo "🚀 首次启动,正在创建容器..."
|
||||
docker-compose -f docker-compose.postgres.yml up -d
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "⏳ 等待数据库启动..."
|
||||
sleep 3
|
||||
|
||||
# 检查数据库是否就绪
|
||||
MAX_RETRIES=30
|
||||
RETRY_COUNT=0
|
||||
|
||||
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
|
||||
if docker exec st-postgres pg_isready -U st_user -d st_dev &> /dev/null; then
|
||||
echo "✅ 数据库已就绪!"
|
||||
break
|
||||
fi
|
||||
|
||||
RETRY_COUNT=$((RETRY_COUNT + 1))
|
||||
echo " 等待中... ($RETRY_COUNT/$MAX_RETRIES)"
|
||||
sleep 2
|
||||
done
|
||||
|
||||
if [ $RETRY_COUNT -eq $MAX_RETRIES ]; then
|
||||
echo "❌ 数据库启动超时,请检查日志:"
|
||||
echo " docker-compose -f docker-compose.postgres.yml logs postgres"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "=========================================="
|
||||
echo "✨ PostgreSQL 启动成功!"
|
||||
echo "=========================================="
|
||||
echo ""
|
||||
echo "📝 数据库连接信息:"
|
||||
echo " Host: localhost"
|
||||
echo " Port: 5432"
|
||||
echo " Database: st_dev"
|
||||
echo " Username: st_user"
|
||||
echo " Password: st_password"
|
||||
echo ""
|
||||
echo "🌐 pgAdmin 管理界面:"
|
||||
echo " URL: http://localhost:5050"
|
||||
echo " Email: admin@st.local"
|
||||
echo " Pass: admin123"
|
||||
echo ""
|
||||
echo "📊 常用命令:"
|
||||
echo " 查看日志: docker-compose -f docker-compose.postgres.yml logs -f"
|
||||
echo " 停止服务: $0 stop"
|
||||
echo " 重启服务: $0 restart"
|
||||
echo " 查看状态: docker-compose -f docker-compose.postgres.yml ps"
|
||||
echo " 进入数据库: docker exec -it st-postgres psql -U st_user -d st_dev"
|
||||
echo ""
|
||||
echo "=========================================="
|
||||
41
deploy/stop-postgres.sh
Normal file
41
deploy/stop-postgres.sh
Normal file
@@ -0,0 +1,41 @@
|
||||
#!/bin/bash
|
||||
|
||||
# ====================================================
|
||||
# PostgreSQL Docker 停止脚本
|
||||
# ====================================================
|
||||
|
||||
set -e
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
echo "=========================================="
|
||||
echo "停止 PostgreSQL 服务"
|
||||
echo "=========================================="
|
||||
echo ""
|
||||
|
||||
# 检查参数
|
||||
if [ "$1" == "--clean" ] || [ "$1" == "-c" ]; then
|
||||
echo "⚠️ 警告: 将删除所有数据(包括数据卷)"
|
||||
read -p "确定要继续吗?(yes/no): " confirm
|
||||
|
||||
if [ "$confirm" != "yes" ]; then
|
||||
echo "操作已取消"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "🗑️ 停止并删除所有内容..."
|
||||
docker-compose -f docker-compose.postgres.yml down -v
|
||||
echo "✅ 已删除容器和数据卷"
|
||||
else
|
||||
echo "🛑 停止服务(保留数据)..."
|
||||
docker-compose -f docker-compose.postgres.yml stop
|
||||
echo "✅ 服务已停止"
|
||||
echo ""
|
||||
echo "💡 提示:"
|
||||
echo " - 数据已保留,重启后可继续使用"
|
||||
echo " - 完全删除(包括数据): $0 --clean"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "=========================================="
|
||||
Reference in New Issue
Block a user