PostgreSQL 9.13 入门


一直想为 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脚本里面已经做了些初始化的工作,例如:

  1. su postgres -c "$PG_ROOT/bin/initdb -D $PG_ROOT/data && exit"    

我们先输入一些数据以供后面查询

  1. -- Database: bpsimple   
  2.   
  3.   
  4. -- DROP DATABASE bpsimple;   
  5.   
  6.   
  7. CREATE DATABASE bpsimple  
  8.   WITH OWNER = postgres  
  9.        ENCODING = 'UTF8'  
  10.        TABLESPACE = pg_default  
  11.        LC_COLLATE = 'en_US.UTF-8'  
  12.        LC_CTYPE = 'en_US.UTF-8'  
  13.        CONNECTION LIMIT = -1;  
  14. <pre name="code" class="sql">-- Table: item   
  15. -- DROP TABLE item;   
  16.   
  17. CREATE TABLE item  
  18. (  
  19.   item_id serial NOT NULL,  
  20.   description character varying(64) NOT NULL,  
  21.   cost_price numeric(7,2),  
  22.   sell_price numeric(7,2),  
  23.   CONSTRAINT item_pk PRIMARY KEY (item_id )  
  24. )  
  25. WITH (  
  26.   OIDS=FALSE  
  27. );  
  28. ALTER TABLE item  
  29.   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

  1. <?php  
  2.   
  3. class dbconn {  
  4.   
  5.     private $linkid;      // PostgreSQL link identifier  
  6.     private $host;        // PostgreSQL server host  
  7.     private $db;          // PostgreSQL database  
  8.     private $user;        // PostgreSQL user  
  9.     private $passwd;      // PostgreSQL password  
  10.     private $result; // Query result  
  11.     private $querycount; //Total queries excuted  
  12.   
  13.     /* Class constructor. Initializes the $host, $user, $passwd  
  14.       and $db fields. */  
  15.   
  16.     function __construct($host, $db, $user, $passwd) {  
  17.         $this->host = $host;  
  18.         $this->user = $user;  
  19.         $this->passwd = $passwd;  
  20.         $this->db = $db;  
  21.     }  
  22.   
  23.     /* Connects to the PostgreSQL Database */  
  24.   
  25.     function connect() {  
  26.         try {  
  27.             $this->linkid = @pg_connect("host=$this->host dbname=$this->db  
  28.             user=$this->user password=$this->passwd");  
  29.             if (!$this->linkid)  
  30.                 throw new Exception("Could not connect to PostgreSQL server.");  
  31.         } catch (Exception $e) {  
  32.             die($e->getMessage());  
  33.         }  
  34.     }  
  35.   
  36.     /* Execute database query. */  
  37.   
  38.     function query($query) {  
  39.         try {  
  40.             $this->result = @pg_query($this->linkid, $query);  
  41.             if (!$this->result)  
  42.                 throw new Exception("The database query failed.");  
  43.         } catch (Exception $e) {  
  44.             echo $e->getMessage();  
  45.         }  
  46.         $this->querycount++;  
  47.         return $this->result;  
  48.     }  
  49.   
  50.     /* Determine total rows affected by query. */  
  51.   
  52.     function affectedRows() {  
  53.         $count = @pg_affected_rows($this->linkid);  
  54.         return $count;  
  55.     }  
  56.   
  57.     /* Determine total rows returned by query */  
  58.   
  59.     function numRows() {  
  60.         $count = @pg_num_rows($this->result);  
  61.         return $count;  
  62.     }  
  63.   
  64.     /* Return query result row as an object. */  
  65.   
  66.     function fetchObject() {  
  67.         $row = @pg_fetch_object($this->result);  
  68.         return $row;  
  69.     }  
  70.   
  71.     /* Return query result row as an indexed array. */  
  72.   
  73.     function fetchRow() {  
  74.         $row = @pg_fetch_row($this->result);  
  75.         return $row;  
  76.     }  
  77.   
  78.     /* Return query result row as an associated array. */  
  79.   
  80.     function fetchArray() {  
  81.         $row = @pg_fetch_array($this->result);  
  82.         return $row;  
  83.     }  
  84.   
  85.     /* Return total number of queries executed during  
  86.       lifetime of this object. Not required, but  
  87.       interesting nonetheless. */  
  88.   
  89.     function numQueries() {  
  90.         return $this->querycount;  
  91.     }  
  92.   
  93. }  
  94. ?>  
  • 1
  • 2
  • 下一页

相关内容