C.W.K.
Stream
Lesson 01 of 10 · published

Python 과 Node.js 에서 연결

~12 min · apps, drivers

Level 0스키마 새싹
0 XP0/86 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

마법 아닌 드라이버

모든 언어가 Postgres 드라이버 가짐. 최고 (Python psycopg, Node pg, Rust sqlx, Go pgx, C libpq) 가 wire 프로토콜 직접 말함, binary 파라미터 인코딩 지원, connection 풀과 통합. ORM 과 query builder 모두 이거 위에 올라감.

Connection string

표준 URL 형태: postgresql://user:pass@host:5432/dbname?sslmode=require. 부분 외워. 같은 string 이 psql, 어느 드라이버, pgAdmin, 어디서나 동작.

항상 파라미터화

드라이버 레벨 파라미터 바인딩이 SQL injection 의 주된 방어. 유저 입력을 SQL string 에 절대 연결하지 마. 파라미터로 전달; 드라이버와 DB 가 escaping 정확히 처리.

Code

psycopg (Python)·python
import psycopg
with psycopg.connect("postgresql://app:secret@localhost:5432/mydb") as conn:
    with conn.cursor() as cur:
        cur.execute(
            "INSERT INTO users (name, email) VALUES (%s, %s) RETURNING id",
            ("Alice", "alice@example.com"),
        )
        new_id = cur.fetchone()[0]
pg (Node.js)·javascript
import pg from 'pg';
const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });

const { rows } = await pool.query(
  'INSERT INTO users (name, email) VALUES ($1, $2) RETURNING id',
  ['Alice', 'alice@example.com']
);
const newId = rows[0].id;
항상 파라미터화 — 절대 연결 금지·python
# 위험 — SQL injection 시한폭탄
cur.execute(f"SELECT * FROM users WHERE email = '{email}'")

# 안전 — 드라이버가 정확히 escape
cur.execute("SELECT * FROM users WHERE email = %s", (email,))

External links

Exercise

좋아하는 언어로 20-line 스크립트: Postgres 연결, 파라미터화 SELECT 실행, 행 출력. 같은 스크립트를 string 연결로; 의식적으로 버그 도입 얼마나 쉬운지 느껴봐.

Progress

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

댓글 0

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

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