I always wanted to make some contributions to PostgreSQL. Today I am free, so I wrote a tutorial on getting started with PostgreSQL 9.13...
On deployment, you can move here...
PHP 5.4.10 + Nginx 1.0.12 + PostgreSQL 9.1.3 source code compilation automated deployment of Version 2
Bytes -----------------------------------------------------------------------------------------
| System | CentOS 5.7.
Bytes -----------------------------------------------------------------------------------------
| Db| PostgreSQL 9.13
Bytes -----------------------------------------------------------------------------------------
Some initialization work has been done in the lnpp script, for example:
- Su postgres-c "$ PG_ROOT/bin/initdb-D $ PG_ROOT/data & exit"
Enter some data for later query.
- -- Database: bpsimple
- -- Drop database bpsimple;
- CREATE DATABASEBpsimple
- WITHOWNER = ipvs
- ENCODING ='Utf8'
- TABLESPACE = pg_default
- LC_COLLATE ='En _ US.UTF-8'
- LC_CTYPE ='En _ US.UTF-8'
- CONNECTIONLIMIT =-1;
- <PreName="Code"Class ="SQL">-- Table: item
- -- Drop table item;
- CREATE TABLEItem
- (
- Item_id serialNOT NULL,
- DescriptionCharacter Varying(64)NOT NULL,
- Cost_priceNumeric(7,2 ),
- Sell_priceNumeric(7,2 ),
- CONSTRAINTItem_pkPRIMARY KEY(Item_id)
- )
- WITH(
- OIDS =FALSE
- );
- ALTER TABLEItem
- OWNERTONeil;
I copied the above SQL pane directly from pgadmin 3, which is the existing data on my simulator, so the above statements have not been tested!
If there is a problem with the http://www.postgresql.org/docs/9.1/interactive/index.html, you can have a manual!
Next, we need to configure postgresql for external access...
Access authorization first...
# Vim $ PG_ROOT/data/pg_hda.conf
Host bpsimple neil all trust
# Vim postgresql. conf
Listen_addresses = '*'
Port = 5432
After setting the listening port, restart postgresql...
Su $ PGUSER-c "$ PGCTL stop-d' $ pgdata'-m fast"
Su $ PGUSER-c "$ PGDAEMON-D '$ pgdata' &" >>$ PGLOG 2> & 1
The specific environment variables depend on different hosts. Well, the topic starts. First, write a pg class...
# 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 ("cocould 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
- Lifetime of this object. Not required,
- Interesting nonetheless .*/
- Function numQueries (){
- Return $ this-> querycount;
- }
- }
- ?>