[PostgreSQL] psycopg2 연결

Ja L·2023년 3월 14일
0

[PostgreSQL] Operation

목록 보기
2/35
python 라이브러리인 psycopg2를 이용해 PostgreSQL DB에 연결하는 과정을 기록

pip3 업데이트

$ pip3 install --upgrade pip

psycopg2 설치

$ pip3 install psycopg2-binary

터미널에서 PostgreSQL을 실행시킨다.

$ pg_ctl start 

터미널에서 python으로 접속

$ python3
import psycopg2 

conn = psycopg2.connect(
database="postgres",
host="localhost",
user="agens",
password=""
)

cur=conn.cursor()

cur.execute("select * from people limit 5 ;")

cur.fetchone()
# (1, datetime.date(1979, 8, 10), '010-0164-4650', '대구', datetime.date(2023, 3, 9))

cur.close()
conn.close()
profile
DB Engineer

0개의 댓글