Skip to content

Instantly share code, notes, and snippets.

@kse0202
Last active June 28, 2021 05:33
Show Gist options
  • Select an option

  • Save kse0202/af6d8cd1072d26cc0286b573faddecae to your computer and use it in GitHub Desktop.

Select an option

Save kse0202/af6d8cd1072d26cc0286b573faddecae to your computer and use it in GitHub Desktop.
Jupyter Notebook에서 python으로 PostgreSQL DB 접속하는 방법 (psycopg2 이용)
# 패키지 불러오기

#-*-coding:utf-8
import psycopg2
import pandas as pd
import csv

import sql
from sqlalchemy import create_engine
## 서버에 연결하는 방법 

#아래 정보를 입력 
user = 'xxx' # postgresql DB user ID
password = 'xxx' # password
host_product = 'IP address' # 서버 IP 
dbname = 'xxxx' # DB Name
port='5432' # port number

product_connection_string = "dbname={dbname} user={user} host={host} password={password} port={port}".format(dbname=dbname,
                                    user=user,
                                    host=host_product,
                                    password=password,
                                    port=port)    
try:
    conn = psycopg2.connect(product_connection_string)
except:
    print("I am unable to connect to the database")

cur = conn.cursor() ## conn, cur 커밋 날릴때 마다 해줘야함
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment