import duckdb
# (1) DuckDB 연결 — 메모리 또는 파일 DB
con = duckdb.connect() # 메모리 기반
# 또는
# con = duckdb.connect("mydb.duckdb") # 파일 기반
# (2) PostgreSQL extension 설치 / 로드 (필요한 경우)
con.execute("INSTALL postgres;")
con.execute("LOAD postgres;")
# (3) PostgreSQL DB를 ATTACH
# 예: 로컬의 PostgreSQL 인스턴스, 기본 스키마 public
con.execute("""
ATTACH 'dbname=postgres user=postgres host=127.0.0.1 port=5432 password=mypassword'
AS pg_db
(TYPE postgres)
""")
# (4) 연결된 PostgreSQL 테이블을 쿼리
# 예: pg_db.public.mytable 라는 테이블이 있다면
res = con.execute("SELECT * FROM pg_db.company LIMIT 5").fetchdf()
print(res)
# (5) (선택) PostgreSQL 쪽에 데이터를 쓰기
# con.execute("""
# INSERT INTO pg_db.new_table (col1, col2)
# VALUES (1, 'abc')
# """)