C.W.K.
Stream
Lesson 02 of 05 · published

pgvector 설치 + vector 컬럼 정의

~18 min · pgvector, setup, sql

Level 0Scout
0 XP0/41 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

0에서 2분

데이터베이스당 한 번 extension 설치, 그 다음 vector(N) 컬럼 추가. 차원은 컬럼 생성 시점에 고정 + 임베딩 모델과 정확히 매칭해야. 이거 틀리면 insert 가 runtime error.

storage 특성

1024차원 float vector = 디스크에 4 KB. 100만 청크 ≈ 인덱스 전 4 GB; HNSW 와 함께면 1.5–2배 예상. Postgres 가 commodity 하드웨어에서 fine. disk 그에 맞게 plan.

Code

pgvector 띄우기·sql
-- 대상 데이터베이스의 psql 안에서 실행
CREATE EXTENSION IF NOT EXISTS vector;

CREATE TABLE chunks (
    id          bigserial PRIMARY KEY,
    source      text        NOT NULL,
    chunk_index int         NOT NULL,
    text        text        NOT NULL,
    metadata    jsonb       DEFAULT '{}'::jsonb,
    embedding   vector(1024) NOT NULL,
    updated_at  timestamptz DEFAULT now()
);

CREATE INDEX ON chunks USING gin (metadata jsonb_path_ops);
CREATE INDEX ON chunks (source, chunk_index);
Python 에서 청크 insert·python
import psycopg
from pgvector.psycopg import register_vector

conn = psycopg.connect('postgresql://localhost/myapp')
register_vector(conn)

with conn.cursor() as cur:
    cur.execute(
        'INSERT INTO chunks (source, chunk_index, text, metadata, embedding) '
        'VALUES (%s, %s, %s, %s, %s)',
        (chunk['metadata']['source'], chunk['metadata']['chunk_index'],
         chunk['text'], json.dumps(chunk['metadata']), vec),
    )
conn.commit()

External links

Exercise

로컬 Postgres 띄워 (Docker, brew, 본인 flavor 뭐든). pgvector 설치. 위 chunks 테이블 생성. Python 에서 row 50개 insert. SELECT count(*) 으로 확인.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.