LXXX. 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_affected_rows -- Returns number of affected records(tuples)
pg_cancel_query --  Cancel async query
pg_client_encoding --  Get the client encoding
pg_close -- PostgreSQL과의 접속을 끊는다.
pg_connect -- PostgreSQL에 접속한다.
pg_connection_busy --  Get connection is busy or not
pg_connection_reset --  Reset connection (reconnect)
pg_connection_status --  Get connection status
pg_convert --  Convert associative array value into suitable for SQL statement.
pg_copy_from --  Insert records into a table from an array
pg_copy_to --  Copy a table to an array
pg_dbname -- 데이터베이스의 이름을 돌려준다.
pg_delete --  Delete records.
pg_end_copy -- PostgreSQL 백엔드에 동기화한다.
pg_escape_bytea --  Escape binary for bytea type
pg_escape_string --  Escape string for text/char type
pg_fetch_array -- 데이터베이스의 행을 배열로 가져온다.
pg_fetch_object -- 데이터베이스의 행을 오브젝트로 가져온다.
pg_fetch_result -- Returns values from a result resource
pg_fetch_row -- 데이터베이스의 행을 숫자를 인덱스로 하는 배열로 가져온다.
pg_field_is_null -- Test if a field is NULL
pg_field_name -- Returns the name of a field
pg_field_num -- Returns the field number of the named field
pg_field_prtlen -- Returns the printed length
pg_field_size --  Returns the internal storage size of the named field
pg_field_type --  Returns the type name for the corresponding field number
pg_free_result -- Free result memory
pg_get_result --  Get asynchronous query result
pg_host --  접속된 PostgreSQL 호스트의 주소를 돌려준다.
pg_insert --  Insert array into table.
pg_last_error -- Get the last error message string of a connection
pg_last_notice --  Returns the last notice message from PostgreSQL server
pg_last_oid -- Returns the last object's oid
pg_lo_close -- Close a large object
pg_lo_create -- Create a large object
pg_lo_export -- Export a large object to file
pg_lo_import -- Import a large object from file
pg_lo_open -- Open a large object
pg_lo_read_all --  Read a entire large object and send straight to browser
pg_lo_read -- Read a large object
pg_lo_seek --  Seeks position of large object
pg_lo_tell --  Returns current position of large object
pg_lo_unlink -- Delete a large object
pg_lo_write -- Write a large object
pg_metadata --  Get metadata for table.
pg_num_fields -- Returns the number of fields
pg_num_rows -- 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_query -- Execute a query
pg_result_error --  Get error message associated with result
pg_result_status --  Get status of query result
pg_select --  Select records.
pg_send_query --  Send asynchronous query
pg_set_client_encoding --  Set 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
pg_update --  Update table.