zioinfo-mail/pgvector_grant.py
DESKTOP-TKLFCPR\ython 777e027553 refactor(structure): move app -> workspace/guardia-messenger
Permission denied on git mv, used robocopy instead.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-31 23:53:57 +09:00

67 lines
2.0 KiB
Python

import paramiko, sys
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('101.79.17.164', username='root', password='1q2w3e!Q', timeout=30)
def run(label, cmd, timeout=60):
print(f'\n[{label}]')
_, o, e = client.exec_command(cmd, timeout=timeout)
print(o.read().decode('utf-8', errors='replace').strip()[:300])
# guardia 유저에게 권한 부여
run('권한 부여', """
sudo -u postgres psql guardia_db -c "
GRANT ALL ON TABLE tb_vector_sr, tb_vector_kb, tb_vector_scrap TO guardia;
GRANT ALL ON SEQUENCE tb_vector_sr_id_seq, tb_vector_kb_id_seq, tb_vector_scrap_id_seq TO guardia;
ALTER TABLE tb_vector_sr OWNER TO guardia;
ALTER TABLE tb_vector_kb OWNER TO guardia;
ALTER TABLE tb_vector_scrap OWNER TO guardia;
" 2>&1
echo OK
""")
# 재테스트
run('연동 테스트', """
/opt/guardia/venv/bin/python3 -c "
import psycopg2
from pgvector.psycopg2 import register_vector
import numpy as np
conn = psycopg2.connect(
host='localhost', port=5432,
dbname='guardia_db', user='guardia', password='G@urd1a_2026!'
)
register_vector(conn)
cur = conn.cursor()
# 테스트 벡터 삽입
test_emb = np.random.rand(768).tolist()
cur.execute(
'INSERT INTO tb_vector_sr (sr_id, title, content, embedding) VALUES (%s,%s,%s,%s) ON CONFLICT DO NOTHING',
('SR-TEST-001', 'pgvector 연동 테스트', 'pgvector 설치 확인', test_emb)
)
conn.commit()
# 유사 벡터 검색
query_emb = np.random.rand(768).tolist()
cur.execute(
'SELECT sr_id, title, embedding <=> %s AS distance FROM tb_vector_sr ORDER BY distance LIMIT 1',
(query_emb,)
)
row = cur.fetchone()
print('SR:', row[0], '| 거리:', round(float(row[2]),4))
cur.close(); conn.close()
print('pgvector 연동 성공!')
" 2>&1
""")
# LangChain pgvector 패키지도 설치
run('LangChain pgvector 설치', """
/opt/guardia/venv/bin/pip install langchain-postgres -q && echo "langchain-postgres OK"
""")
client.close()
print('\n=== 완료 ===')