一直想為 PostgreSQL 做點貢獻,今天終於有空了, 於是寫一個PostgreSQL 9.13 入門的教程 ...
部署上可以移步這裡 ...
PHP 5.4.10 + Nginx 1.0.12 + PostgreSQL 9.1.3 源碼編譯自動化部署第二版
-----------------------------------------------------------------------------------------
| System | CentOS 5.7
-----------------------------------------------------------------------------------------
| DB | PostgreSQL 9.13
-----------------------------------------------------------------------------------------
lnpp指令碼裡面已經做了些初始化的工作,例如:
- su postgres -c "$PG_ROOT/bin/initdb -D $PG_ROOT/data && exit"
我們先輸入一些資料以供後面查詢
- -- Database: bpsimple
-
-
- -- DROP DATABASE bpsimple;
-
-
- CREATE DATABASE bpsimple
- WITH OWNER = postgres
- ENCODING = 'UTF8'
- TABLESPACE = pg_default
- LC_COLLATE = 'en_US.UTF-8'
- LC_CTYPE = 'en_US.UTF-8'
- CONNECTION LIMIT = -1;
- <pre name="code" class="sql">-- Table: item
- -- DROP TABLE item;
-
- CREATE TABLE item
- (
- item_id serial NOT NULL,
- description character varying(64) NOT NULL,
- cost_price numeric(7,2),
- sell_price numeric(7,2),
- CONSTRAINT item_pk PRIMARY KEY (item_id )
- )
- WITH (
- OIDS=FALSE
- );
- ALTER TABLE item
- OWNER TO neil;
以上我直接從pgadmin 3 上的sql pane copy 下來的,是我模擬器上的現有資料,所以以上語句沒有經過測試 !
http://www.postgresql.org/docs/9.1/interactive/index.html 有問題的話,可以手冊一下!
接下來我們還要對postgresql 進行一些配置已經進行外部的訪問 ...
先進行訪問授權 ...
#vim $PG_ROOT/data/pg_hda.conf
host bpsimple neil all trust
#vim postgresql.conf
listen_addresses = '*'
port = 5432
設定完監聽連接埠後我們重啟一下postgresql ...
su $PGUSER -c "$PGCTL stop -D '$PGDATA' -m fast"
su $PGUSER -c "$PGDAEMON -D '$PGDATA' &" >>$PGLOG 2>&1
具體環境變數視不同機子而定,好吧,主題開始,首先編寫一個pg類 ...
#vim ./pgphp/dbconn.php
- <?php
-
- class dbconn {
-
- private $linkid; // PostgreSQL link identifier
- private $host; // PostgreSQL server host
- private $db; // PostgreSQL database
- private $user; // PostgreSQL user
- private $passwd; // PostgreSQL password
- private $result; // Query result
- private $querycount; //Total queries excuted
-
- /* Class constructor. Initializes the $host, $user, $passwd
- and $db fields. */
-
- function __construct($host, $db, $user, $passwd) {
- $this->host = $host;
- $this->user = $user;
- $this->passwd = $passwd;
- $this->db = $db;
- }
-
- /* Connects to the PostgreSQL Database */
-
- function connect() {
- try {
- $this->linkid = @pg_connect("host=$this->host dbname=$this->db
- user=$this->user password=$this->passwd");
- if (!$this->linkid)
- throw new Exception("Could not connect to PostgreSQL server.");
- } catch (Exception $e) {
- die($e->getMessage());
- }
- }
-
- /* Execute database query. */
-
- function query($query) {
- try {
- $this->result = @pg_query($this->linkid, $query);
- if (!$this->result)
- throw new Exception("The database query failed.");
- } catch (Exception $e) {
- echo $e->getMessage();
- }
- $this->querycount++;
- return $this->result;
- }
-
- /* Determine total rows affected by query. */
-
- function affectedRows() {
- $count = @pg_affected_rows($this->linkid);
- return $count;
- }
-
- /* Determine total rows returned by query */
-
- function numRows() {
- $count = @pg_num_rows($this->result);
- return $count;
- }
-
- /* Return query result row as an object. */
-
- function fetchObject() {
- $row = @pg_fetch_object($this->result);
- return $row;
- }
-
- /* Return query result row as an indexed array. */
-
- function fetchRow() {
- $row = @pg_fetch_row($this->result);
- return $row;
- }
-
- /* Return query result row as an associated array. */
-
- function fetchArray() {
- $row = @pg_fetch_array($this->result);
- return $row;
- }
-
- /* Return total number of queries executed during
- lifetime of this object. Not required, but
- interesting nonetheless. */
-
- function numQueries() {
- return $this->querycount;
- }
-
- }
- ?>