PostgreSQLデータベースはオープンソースのプロダクツであり、無料で 使用可能できます。Postgresは元々UCB(カリフォルニア大学バークレイ校) コンピュータ・サイエンス学部で開発されたものです。このPostgresは、 現在、いくつかの商用データベースにおいてサポートされつつあるオブジェク トリレーショナルデータベース的概念の多くの先駆けでした。 Postgresは、SQL92/SQL3言語サポート、トランザクションインテグリティ、 拡張可能な型を提供しています。PostgreSQLは、Postgresのオリジナルの バークレイ・コードのオープンソースの子孫にあたります。
PostgreSQLサポートを使用するには、PostgreSQL 6.5以降が必要です。 PostgreSQL 7.0以降ではPostgreSQLモジュールの全ての機能を使用可能 です。PostgreSQLは、マルチバイト文字エンコーディングを含む多くの 文字エンコーディングをサポートしています。現在のバージョン及び PostgreSQLに関するより詳細な情報は、 http://www.postgresql.org/と http://techdocs.postgresql.org/ で入手可能です。
PostgreSQLサポートを利用可能とするには、PHPコンパイル時に --with-pgsql[=DIR] を指定することが必要です。 共有オブジェクトモジュールが利用可能な場合、php.ini の extension ディレクティブ または dl() 関数によりPostgreSQLモジュール をロードすることが可能です。
これらの関数の動作は、php.iniの設定により変化します。
表 1. PostgreSQL設定オプション
名前 | デフォルト | 変更の可否 |
---|---|---|
pgsql.allow_persistent | "1" | PHP_INI_SYSTEM |
pgsql.max_persistent | "-1" | PHP_INI_SYSTEM |
pgsql.max_links | "-1" | PHP_INI_SYSTEM |
pgsql.auto_reset_persistent | "0" | PHP_INI_SYSTEM |
pgsql.ignore_notice | "0" | PHP_INI_ALL |
pgsql.log_notice | "0" | PHP_INI_ALL |
以下に設定ディレクティブに関す る簡単な説明を示します。
持続的なPostgres接続を可能にするかどうか。
プロセス毎の持続的Postgres接続の最大数。
持続的接続を含むプロセス毎のPostgres接続の最大数。
Detect broken persistent links with pg_pconnect(). Needs a little overhead.
Whether or not to ignore PostgreSQL backend notices.
Whether or not to log PostgreSQL backends notice messages. The PHP directive pgsql.ignore_notice must be off in order to log notice messages.
警告 |
通知メッセージを処理するコードにバグがあるため、PHP 4.0.6で PostgreSQLモジュールを使用することは推奨されません。 4.1.0以降を使用してください。 |
警告 | ||||||||||||||||||||||||||||||||||||||||||||
PostgreSQLモジュールの関数名は、現在のコーディング標準に準拠する ために 4.2.0以降変更されます。新しい関数名の多くには、 pg_lo_open()のようにアンダースコアが付きます。 いくつかの関数は、pg_exec()からpg_query()というように、統一を とるために関数名が変更されます。 古い名前も4.2.0とそれに続くいくつかのバージョンで使用可能ですが、 将来的には削除される可能性があります。 表 2. 変更された関数名
従来のpg_connect()()/pg_pconnect()() 構文は、将来的に非同期接続 機能をサポートするために過去のものとなる可能性があります。 pg_connect()、pg_pconnect() では、接続文字列を使用してください。 |
全ての関数が全ての構築環境でサポートされるわけではありません。サ ポートされる関数は、使用するlibpq(PostgreSQLのCクライアントインター フェイス)のバージョンとlibpqのコンパイル方法に依存します。足りな い関数がある場合、libpqはその関数を必要とする機能をサポートしませ ん。
接続するPostgreSQLサーバーよりも新しいlibpqを使用することも重要で す。使用するlibpqのバージョンがPostgreSQLサーバが期待するよりも古 い場合、問題を生じる可能性があります。
バージョン6.3(1998/3/2)以降、PostgreSQLはUNIXドメインソケットを 使用しています。これらの新しいコネクションの使用例を以下に示す表に 示します。このソケットは/tmp/.s.PGSQL.5432に あります。このオプションはpostmasterに対して'- i'フラグを指定することで有効となります。これは "Unixドメインソケッ ト以外のTCP/IPソケットもlisten する"という意味です。
表 3. postmasterとPHP
postmaster | PHP | ステータス |
---|---|---|
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 |
PostgreSQL サーバへの接続は、以下のようにコマンド文字列の中に 値の組を羅列することにより、確立することができます。 $conn = pg_Connect("host=myHost port=myPort tty=myTTY options=myOptions dbname=myDB user=myUser password=myPassword");
以前の構文: $conn = pg_connect ("host", "port", "options", "tty", "dbname") は古い構文であり、推奨されません。
環境変数は、PostgreSQLサーバ/クライアントの動作に影響を与えます。 例えば、PostgreSQLモジュールは、ホスト名が接続文字列で省略された場 合、環境変数PGHOSTを探します。サポートされる環境変数は、バージョン 毎に異なります。詳細は、PostgreSQLプログラマ用マニュアル(libpq - 環境変数)を参照ください。
適切なユーザ用に環境変数が設定されていることを確認してください。 カレントのプロセスで利用可能な環境変数を確認するには、 $_ENV または getenv() を使用してください。
これらの定数は、この拡張モジュールで定義されており、 この拡張モジュールがPHP内部にコンパイルされているか実行時に動的にロー ドされるかのどちらかの場合のみ使用可能です。
Passed to pg_fetch_array(). Return an associative array of field names and values.
Passed to pg_fetch_array(). Return a numerically indexed array of field numbers and values.
Passed to pg_fetch_array(). Return an array of field values that is both numerically indexed (by field number) and associated (by field name).
Passed to pg_connect() to force the creation of a new connection, rather then re-using an existing identical connection.
Returned by pg_connection_status() indicating that the database connection is in an invalid state.
Returned by pg_connection_status() indicating that the database connection is in a valid state.
Passed to pg_lo_seek(). Seek operation is to begin from the start of the object.
Passed to pg_lo_seek(). Seek operation is to begin from the current position.
Passed to pg_lo_seek(). Seek operation is to begin from the end of the object.
Returned by pg_result_status(). The string sent to the server was empty.
Returned by pg_result_status(). Successful completion of a command returning no data.
Returned by pg_result_status(). Successful completion of a command returning data (such as a SELECT or SHOW).
Returned by pg_result_status(). Copy Out (from server) data transfer started.
Returned by pg_result_status(). Copy In (to server) data transfer started.
Returned by pg_result_status(). The server's response was not understood.
Returned by pg_result_status(). A nonfatal error (a notice or warning) occurred.
Returned by pg_result_status(). A fatal error occurred.
Returned by pg_transaction_status(). Connection is currently idle, not in a transaction.
Returned by pg_transaction_status(). A command is in progress on the connection. A query has been sent via the connection and not yet completed.
Returned by pg_transaction_status(). The connection is idle, in a transaction block.
Returned by pg_transaction_status(). The connection is idle, in a failed transaction block.
Returned by pg_transaction_status(). The connection is bad.
Passed to pg_result_error_field(). The severity; the field contents are ERROR, FATAL, or PANIC (in an error message), or WARNING, NOTICE, DEBUG, INFO, or LOG (in a notice message), or a localized translation of one of these. Always present.
Passed to pg_result_error_field(). The SQLSTATE code for the error. The SQLSTATE code identifies the type of error that has occurred; it can be used by front-end applications to perform specific operations (such as error handling) in response to a particular database error. This field is not localizable, and is always present.
Passed to pg_result_error_field(). The primary human-readable error message (typically one line). Always present.
Passed to pg_result_error_field(). Detail: an optional secondary error message carrying more detail about the problem. May run to multiple lines.
Passed to pg_result_error_field(). Hint: an optional suggestion what to do about the problem. This is intended to differ from detail in that it offers advice (potentially inappropriate) rather than hard facts. May run to multiple lines.
Passed to pg_result_error_field(). A string containing a decimal integer indicating an error cursor position as an index into the original statement string. The first character has index 1, and positions are measured in characters not bytes.
Passed to pg_result_error_field(). This is defined the same as the PG_DIAG_STATEMENT_POSITION field, but it is used when the cursor position refers to an internally generated command rather than the one submitted by the client. The PG_DIAG_INTERNAL_QUERY field will always appear when this field appears.
Passed to pg_result_error_field(). The text of a failed internally-generated command. This could be, for example, a SQL query issued by a PL/pgSQL function.
Passed to pg_result_error_field(). An indication of the context in which the error occurred. Presently this includes a call stack traceback of active procedural language functions and internally-generated queries. The trace is one entry per line, most recent first.
Passed to pg_result_error_field(). The file name of the PostgreSQL source-code location where the error was reported.
Passed to pg_result_error_field(). The line number of the PostgreSQL source-code location where the error was reported.
Passed to pg_result_error_field(). The name of the PostgreSQL source-code function reporting the error.
Passed to pg_set_error_verbosity(). Specified that returned messages include severity, primary text, and position only; this will normally fit on a single line.
Passed to pg_set_error_verbosity(). The default mode produces messages that include the above plus any detail, hint, or context fields (these may span multiple lines).
Passed to pg_set_error_verbosity(). The verbose mode includes all available fields.
Passed to pg_result_status(). Indicates that numerical result code is desired.
Passed to pg_result_status(). Indicates that textual result command tag is desired.
Passed to pg_convert(). Ignore default values in the table during conversion.
Passed to pg_convert(). Use SQL NULL in place of an empty string.
Passed to pg_convert(). Ignore conversion of NULL into SQL NOT NULL columns.
PostgreSQL 7.1.0から、textデータ型の最大サイズは1GBとなっています。 古いPostgreSQLのtextデータ型はブロックサイズ(デフォルトは8KB、最大 32KBをコンパイル時に定義可能)の制限を受けていました。
ラージオブジェクト(lo)インターフェースを使用するには、そのクエリーを トランザクション・ブロックの中に入れてやる必要があります。 トランザクション・ブロックはBEGINで始まり、 そのトランザクションが有効な場合はCOMMITまたは ENDで終わります。トランザクションが失敗した場合、 そのトランザクションはROLLBACKまたは ABORTにより閉じる必要があります。