설문조사
PostgreSQL/PPAS 관련 듣고 싶은 교육은


총 게시물 13건, 최근 0 건
   

Python 과 연동하기

글쓴이 : PostgresDBA 날짜 : 2012-12-02 (일) 21:55 조회 : 9063
파이썬 공부한지 한 10여년이 지난것 같은데. 이제야 써먹네요 --;
http://initd.org/psycopg/ 에서 psycopg 라는 PostgreSQL 연동 모듈을 다운받아 설치합니다.

[root@olmaster:/mnt/hgfs/common/]# tar xvfz psycopg2-2.4.5.tar.gz 
[root@olmaster:/mnt/hgfs/common/]# cd psycopg2-2.4.5
[root@olmaster:/mnt/hgfs/common/psycopg2-2.4.5]# export PATH=/usr/pgsql-9.2/bin:$PATH  ## pg_config 유틸 경로 지정

[root@olmaster:/mnt/hgfs/common/psycopg2-2.4.5]# pg_config       ## 설치시에 pg_config 를 이용한다.
BINDIR = /usr/pgsql-9.2/bin
DOCDIR = /usr/share/doc/pgsql
HTMLDIR = /usr/share/doc/pgsql
INCLUDEDIR = /usr/pgsql-9.2/include
PKGINCLUDEDIR = /usr/pgsql-9.2/include
INCLUDEDIR-SERVER = /usr/pgsql-9.2/include/server
LIBDIR = /usr/pgsql-9.2/lib
PKGLIBDIR = /usr/pgsql-9.2/lib
LOCALEDIR = /usr/pgsql-9.2/share/locale
MANDIR = /usr/pgsql-9.2/share/man
SHAREDIR = /usr/pgsql-9.2/share
SYSConFDIR = /etc/sysconfig/pgsql
PGXS = /usr/pgsql-9.2/lib/pgxs/src/makefiles/pgxs.mk
ConFIGURE = '--disable-rpath' '--prefix=/usr/pgsql-9.2' '--includedir=/usr/pgsql-9.2/include' '--mandir=/usr/pgsql-9.2/share/man' '--datadir=/usr/pgsql-9.2/share' '--with-perl' '--with-python' '--with-tcl' '--with-tclconfig=/usr/lib64' '--with-openssl' '--with-pam' '--with-krb5' '--with-gssapi' '--with-includes=/usr/include' '--with-libraries=/usr/lib64' '--enable-nls' '--with-ossp-uuid' '--with-libxml' '--with-libxslt' '--with-ldap' '--with-system-tzdata=/usr/share/zoneinfo' '--sysconfdir=/etc/sysconfig/pgsql' '--docdir=/usr/share/doc' 'CFLAGS=-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -I/usr/include/et' 'CPPFLAGS= -I/usr/include/et'
CC = gcc
CPPFLAGS = -I/usr/include/et -D_GNU_SOURCE -I/usr/include/libxml2 -I/usr/include
CFLAGS = -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -I/usr/include/et -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv
CFLAGS_SL = -fpic
LDFLAGS = -L/usr/lib64 -Wl,--as-needed
LDFLAGS_EX = 
LDFLAGS_SL = 
LIBS = -lpgport -lxslt -lxml2 -lpam -lssl -lcrypto -lgssapi_krb5 -lz -lreadline -lcrypt -ldl -lm 
VERSION = PostgreSQL 9.2.1

-- pyscopg 모듈을 이제 설치합니다.
[root@olmaster:/mnt/hgfs/common/psycopg2-2.4.5]# python setup.py install
running install
........
running install_egg_info
Writing /usr/lib64/python2.6/site-packages/psycopg2-2.4.5-py2.6.egg-info
[root@olmaster:/mnt/hgfs/common/psycopg2-2.4.5]# 


postgres@olmaster:~/oradba]$ cat sample.py 
#!/usr/bin/python
# Copyright (c) PostgresDBA.com

import psycopg2

conn = psycopg2.connect("dbname=scottdb user=scott password=tiger")
cur = conn.cursor()

cur.execute("SELECT ename, empno FROM emp;")
rows = cur.fetchall()

for row in rows:
        print row

cur.close()
conn.close()
[postgres@olmaster:~/oradba]$ chmod 755 sample.py
[postgres@olmaster:~/oradba]$ ./sample.py
('SMITH', 7369)
('ALLEN', 7499)
('WARD', 7521)
('JONES', 7566)
('MARTIN', 7654)
('BLAKE', 7698)
('CLARK', 7782)
('SCOTT', 7788)
('KING', 7839)
('TURNER', 7844)
('ADAMS', 7876)
('JAMES', 7900)
('FORD', 7902)
('MILLER', 7934)
[postgres@olmaster:~/oradba]$ 

오 신기합니다.^^

   

postgresdba.com