Files
iam-service/init.sql
2026-01-29 18:14:47 +08:00

36 lines
1.2 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- 1. 创建 iam_service 专用用户
CREATE USER iam_service_user WITH PASSWORD 'iam_service_password';
-- 2. 创建 iam_service 专用数据库
CREATE DATABASE iam_service_db OWNER iam_service_user;
-- 3. 赋予权限(确保它能在 iam_service_db 库里创建 Schema
GRANT ALL PRIVILEGES ON DATABASE iam_service_db TO iam_service_user;
-- 进入 iam_service_db
-- 1. 启用 UUID 扩展
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- 2. 租户表
CREATE TABLE tenants (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
name VARCHAR(255) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- 3. 用户表 (多租户核心)
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
tenant_id UUID NOT NULL REFERENCES tenants(id),
email VARCHAR(255) NOT NULL,
password_hash VARCHAR(255) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- 4. 关键:创建联合唯一索引
-- 允许不同租户拥有相同的 email但同一租户内 email 必须唯一
CREATE UNIQUE INDEX idx_users_tenant_email ON users(tenant_id, email);
-- 5. 初始化一个测试租户 (方便后续测试)
INSERT INTO tenants (id, name) VALUES ('11111111-1111-1111-1111-111111111111', 'Default Corp');