docs: PostgreSQL 연결 가이드 업데이트

- auth_db → main_db 변경 반영
- 테이블 구조 최신화 (gmail_tokens, conversation_logs)
- 서버별 DB 연결 상태 추가 (51123 직접, 51124 SSH 터널)
- 주요 서비스별 연결 방법 정리
This commit is contained in:
happybell80 2025-08-26 00:23:39 +09:00
parent f65b2d186d
commit b9344ef9f2

View File

@ -55,9 +55,9 @@ sudo pkill -f "ssh.*5433"
### 1. 연결 정보
```
Host: localhost (SSH 터널 사용시)
Port: 5433 (터널 로컬 포트)
Database: auth_db
Host: localhost (SSH 터널 사용시) 또는 192.168.219.45 (직접 연결)
Port: 5433 (터널 로컬 포트) 또는 5432 (직접 연결)
Database: main_db (기존 auth_db는 더 이상 사용 안함)
Username: robeings
Password: robeings
```
@ -67,11 +67,14 @@ Password: robeings
# psql 설치 (Ubuntu/Debian)
sudo apt-get install postgresql-client
# DB 접속
psql postgresql://robeings:robeings@localhost:5433/auth_db
# DB 접속 (51123 서버에서 직접)
psql postgresql://robeings:robeings@localhost:5432/main_db
# 또는 (51124 서버에서 SSH 터널 사용)
psql postgresql://robeings:robeings@localhost:5433/main_db
# 또는
psql -h localhost -p 5433 -U robeings -d auth_db
psql -h localhost -p 5432 -U robeings -d main_db
```
### 3. psql 기본 명령어
@ -107,8 +110,11 @@ pip install psycopg2-binary
import psycopg2
import json
# DB 연결
conn = psycopg2.connect('postgresql://robeings:robeings@localhost:5433/auth_db')
# DB 연결 (51123 서버에서)
conn = psycopg2.connect('postgresql://robeings:robeings@localhost:5432/main_db')
# DB 연결 (51124 서버에서 SSH 터널 사용)
conn = psycopg2.connect('postgresql://robeings:robeings@localhost:5433/main_db')
cur = conn.cursor()
# 쿼리 실행
@ -159,8 +165,14 @@ CREATE TABLE users (
CREATE TABLE gmail_tokens (
id SERIAL PRIMARY KEY,
user_id UUID REFERENCES users(id),
slack_id VARCHAR(100), -- Slack 사용자 ID (새로 추가)
is_equipped BOOLEAN,
token_data JSONB, -- access_token, refresh_token 저장
has_token BOOLEAN, -- token_data 존재 여부
access_token TEXT, -- 개별 컬럼으로 분리
refresh_token TEXT, -- 개별 컬럼으로 분리
token_type VARCHAR,
expires_at FLOAT,
token_data JSONB, -- 레거시 호환용
oauth_config JSONB, -- OAuth 설정 정보
scopes JSONB, -- Gmail API 권한 목록
metadata JSONB,
@ -172,6 +184,35 @@ CREATE TABLE gmail_tokens (
);
```
### 3. conversation_logs 테이블
```sql
CREATE TABLE conversation_logs (
id SERIAL PRIMARY KEY,
robeing_id VARCHAR,
channel_id VARCHAR,
message VARCHAR,
response VARCHAR,
intent VARCHAR,
confidence DOUBLE PRECISION,
timestamp TIMESTAMP,
user_id UUID REFERENCES users(id), -- NULL 허용으로 변경됨
slack_user_id VARCHAR(100) -- Slack ID 직접 저장 (새로 추가)
);
```
### 4. slack_user_mapping 테이블
```sql
CREATE TABLE slack_user_mapping (
id UUID PRIMARY KEY,
slack_user_id VARCHAR(100) NOT NULL,
slack_workspace_id UUID,
user_id UUID REFERENCES users(id),
workspace_member_id UUID,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```
---
## 자주 사용하는 쿼리
@ -181,7 +222,7 @@ CREATE TABLE gmail_tokens (
import psycopg2
import json
conn = psycopg2.connect('postgresql://robeings:robeings@localhost:5433/auth_db')
conn = psycopg2.connect('postgresql://robeings:robeings@localhost:5432/main_db')
cur = conn.cursor()
# Gmail 토큰 상태 확인
@ -254,7 +295,7 @@ def add_gmail_token(slack_user_id, email, access_token, refresh_token):
namespace = uuid.UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8')
user_uuid = str(uuid.uuid5(namespace, slack_user_id))
conn = psycopg2.connect('postgresql://robeings:robeings@localhost:5433/auth_db')
conn = psycopg2.connect('postgresql://robeings:robeings@localhost:5432/main_db')
cur = conn.cursor()
try:
@ -359,8 +400,11 @@ json_data = row[0] # 자동으로 Python dict로 변환됨
`.env` 파일에 추가:
```bash
# PostgreSQL Connection (SSH tunnel)
POSTGRES_CONNECTION_STRING=postgresql://robeings:robeings@localhost:5433/auth_db
# PostgreSQL Connection (51123 서버 직접 연결)
POSTGRES_CONNECTION_STRING=postgresql://robeings:robeings@localhost:5432/main_db
# PostgreSQL Connection (51124 서버 SSH 터널)
POSTGRES_CONNECTION_STRING=postgresql://robeings:robeings@localhost:5433/main_db
# SSH Tunnel Config
SSH_HOST=124.55.18.179
@ -423,4 +467,35 @@ crontab에 추가 (5분마다 체크):
---
*마지막 업데이트: 2025-08-20*
---
## 서버별 DB 연결 상태 (2025-08-26 현재)
### 51123 서버 (192.168.219.45)
- **PostgreSQL**: 로컬 실행 중 (포트 5432)
- **데이터베이스**: main_db
- **직접 연결**: `postgresql://robeings:robeings@localhost:5432/main_db`
### 51124 서버 (192.168.219.52)
- **PostgreSQL**: 없음 (51123 서버 DB 사용)
- **연결 방법**: SSH 터널
- **터널 설정**: `ssh -N -L 5433:localhost:5432 admin@192.168.219.45`
- **연결 문자열**: `postgresql://robeings:robeings@localhost:5433/main_db`
### 주요 서비스별 DB 연결
| 서비스 | 서버 | DB 연결 방법 |
|--------|------|-------------|
| auth-server | 51123 | 직접 연결 (localhost:5432) |
| robeing-gateway | 51123 | 직접 연결 (localhost:5432) |
| rb8001 | 51124 | SSH 터널 (localhost:5433) |
| State Service | 51124 | SSH 터널 (localhost:5433) |
| skill-email | 51124 | SSH 터널 (localhost:5433) |
### 주의사항
- **auth_db는 더 이상 사용하지 않음** (main_db로 통합)
- State Service의 DATABASE_URL이 auth_db를 참조하면 main_db로 수정 필요
- SSH 터널은 시스템 재시작 시 재생성 필요
---
*마지막 업데이트: 2025-08-26*