LXVIII. PostgreSQL functions

Postgres는 본래 버클리대학의 컴퓨터 사이언스 학부에서 학술적인 목적으로 개발되었던 데이터베이스로, 현재 몇몇 상용 데이터베이스들이 채용하고 있는 객체관계 개념을 실험적으로 적용시켜왔다. Postgres는 SQL92/SQL3언어, 트랜잭션간의 무결성 및 기타 확장가능한 형식을 지원한다. PostgreSQL은 이같은 버클리대학의 코드를 근간으로 개발되고 있는 오픈소스의 성과물이다.

PostgreSQL의 배포는 무료이며 최신버전은 www.PostgreSQL.org 에서 구할 수 있다.

PostgreSQL은 버전 6.3(1998년 3월 2일 발표)부터 유닉스 도메인 소켓 (unix domain sockets)을 사용한다. 아래의 테이블은 유닉스 도메인 소켓을 사용한 새로운 데이타베이스 접속예를 나타내고 있다. 소켓의 경로는 /tmp/.s.PGSQL.5432 이다. 이 옵션은 postmaster 에 -i 플래그를 넣어줌으로써 가능하며 그 의미는 "유닉스 도메인 소켓의 요청과 함께 TCP/IP 소켓의 요청도 동시에 기다린다"는 뜻이다.

표 1. Postmaster and PHP

PostmasterPHPStatus
postmaster &pg_connect("dbname=MyDbName");OK
postmaster -i &pg_connect("dbname=MyDbName");OK
postmaster &pg_connect("host=localhost dbname=MyDbName"); Unable to connect to PostgreSQL server: connectDB() failed: Is the postmaster running and accepting TCP/IP (with -i) connection at 'localhost' on port '5432'? in /path/to/file.php3 on line 20.
postmaster -i &pg_connect("host=localhost dbname=MyDbName");OK

PHP로 PostgreSQL에 접속하는 코드의 예제는 다음과 같다: $conn = pg_Connect("host=myHost port=myPort tty=myTTY options=myOptions user=myUser password=myPassword dbname=myDB");

예전에 사용하던 $conn = pg_connect ("host", "port", "options", "tty", "dbname") 은 추천하지 않는다.

PHP에서 PostgreSQL의 Large Object 기능을 사용하려면 그것을 트랜잭션 블럭안에 포함시켜야 한다. 트랜잭션 블럭은 begin 으로 시작해서 commit 혹은 end로 끝난다. 만약 트랜잭션이 실패하였다면 그 실패한 트랜잭션은 반드시 rollback 혹은 abort 로 끝나야 한다.

예 1. Using Large Objects


<?php
    $database = pg_Connect ("dbname=jacarta");
    pg_exec ($database, "begin");
    $oid = pg_locreate ($database);
    echo ("$oid\n");
    $handle = pg_loopen ($database, $oid, "w");
    echo ("$handle\n");
    pg_lowrite ($handle, "gaga");
    pg_loclose ($handle);
    pg_exec ($database, "commit");
?>
     

차례
pg_close -- PostgreSQL과의 접속을 끊는다.
pg_cmdtuples -- 데이터베이스 명령어에 영향을 받은 튜플의 갯수를 돌려준다.
pg_connect -- PostgreSQL에 접속한다.
pg_dbname -- 데이터베이스의 이름을 돌려준다.
pg_end_copy -- PostgreSQL 백엔드에 동기화한다.
pg_errormessage -- 데이터베이스의 에러메시지를 가져온다.
pg_exec -- 쿼리를 실행한다.
pg_fetch_array -- 데이터베이스의 행을 배열로 가져온다.
pg_fetch_object -- 데이터베이스의 행을 오브젝트로 가져온다.
pg_fetch_row -- 데이터베이스의 행을 숫자를 인덱스로 하는 배열로 가져온다.
pg_fieldisnull -- 필드가 NULL인지 검사한다.
pg_fieldname -- 필드의 이름을 돌려준다.
pg_fieldnum -- Returns the field number of the named field
pg_fieldprtlen -- Returns the printed length
pg_fieldsize --  주어진 필드의 내부저장 공간의 크기를 돌려준다.
pg_fieldtype --  주어진 필드번호에 해당하는 데이터형을 돌려준다.
pg_freeresult -- 결과값을 저장했던 메모리를 비운다.
pg_getlastoid -- 마지막 오브젝트 지시자를 돌려준다.
pg_host --  접속된 PostgreSQL 호스트의 주소를 돌려준다.
pg_loclose -- Close a large object
pg_locreate -- Create a large object
pg_loexport -- Export a large object to file
pg_loimport -- Import a large object from file
pg_loopen -- Open a large object
pg_loread -- Read a large object
pg_loreadall --  Read a entire large object and send straight to browser
pg_lounlink -- Delete a large object
pg_lowrite -- Write a large object
pg_numfields -- Returns the number of fields
pg_numrows -- Returns the number of rows
pg_options -- Get the options associated with the connection
pg_pconnect -- Open a persistant PostgreSQL connection
pg_port --  Return the port number associated with the connection
pg_put_line -- Send a NULL-terminated string to PostgreSQL backend
pg_result -- Returns values from a result identifier
pg_set_client_encoding --  Set the client encoding
pg_client_encoding --  Get the client encoding
pg_trace -- Enable tracing a PostgreSQL connection
pg_tty --  Return the tty name associated with the connection
pg_untrace -- Disable tracing of a PostgreSQL connection