PostgreSQL 9.13

Source: Internet
Author: User

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:

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

Enter some data for later query.

  1. -- Database: bpsimple
  2. -- Drop database bpsimple;
  3. CREATE DATABASEBpsimple
  4. WITHOWNER = ipvs
  5. ENCODING ='Utf8'
  6. TABLESPACE = pg_default
  7. LC_COLLATE ='En _ US.UTF-8'
  8. LC_CTYPE ='En _ US.UTF-8'
  9. CONNECTIONLIMIT =-1;
  10. <PreName="Code"Class ="SQL">-- Table: item
  11. -- Drop table item;
  12. CREATE TABLEItem
  13. (
  14. Item_id serialNOT NULL,
  15. DescriptionCharacter Varying(64)NOT NULL,
  16. Cost_priceNumeric(7,2 ),
  17. Sell_priceNumeric(7,2 ),
  18. CONSTRAINTItem_pkPRIMARY KEY(Item_id)
  19. )
  20. WITH(
  21. OIDS =FALSE
  22. );
  23. ALTER TABLEItem
  24. 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

  1. <? Php
  2. Class dbconn {
  3. Private $ linkid; // PostgreSQL link identifier
  4. Private $ host; // PostgreSQL server host
  5. Private $ db; // PostgreSQL database
  6. Private $ user; // PostgreSQL user
  7. Private $ passwd; // PostgreSQL password
  8. Private $ result; // Query result
  9. Private $ querycount; // Total queries excuted
  10. /* Class constructor. Initializes the $ host, $ user, $ passwd
  11. And $ db fields .*/
  12. Function _ construct ($ host, $ db, $ user, $ passwd ){
  13. $ This-> host = $ host;
  14. $ This-> user = $ user;
  15. $ This-> passwd = $ passwd;
  16. $ This-> db = $ db;
  17. }
  18. /* Connects to the PostgreSQL Database */
  19. Function connect (){
  20. Try {
  21. $ This-> linkid = @ pg_connect ("host = $ this-> host dbname = $ this-> db
  22. User = $ this-> user password = $ this-> passwd ");
  23. If (! $ This-> linkid)
  24. Throw new Exception ("cocould not connect to PostgreSQL server .");
  25. } Catch (Exception $ e ){
  26. Die ($ e-> getMessage ());
  27. }
  28. }
  29. /* Execute database query .*/
  30. Function query ($ query ){
  31. Try {
  32. $ This-> result = @ pg_query ($ this-> linkid, $ query );
  33. If (! $ This-> result)
  34. Throw new Exception ("The database query failed .");
  35. } Catch (Exception $ e ){
  36. Echo $ e-> getMessage ();
  37. }
  38. $ This-> querycount ++;
  39. Return $ this-> result;
  40. }
  41. /* Determine total rows affected by query .*/
  42. Function affectedRows (){
  43. $ Count = @ pg_affected_rows ($ this-> linkid );
  44. Return $ count;
  45. }
  46. /* Determine total rows returned by query */
  47. Function numRows (){
  48. $ Count = @ pg_num_rows ($ this-> result );
  49. Return $ count;
  50. }
  51. /* Return query result row as an object .*/
  52. Function fetchObject (){
  53. $ Row = @ pg_fetch_object ($ this-> result );
  54. Return $ row;
  55. }
  56. /* Return query result row as an indexed array .*/
  57. Function fetchRow (){
  58. $ Row = @ pg_fetch_row ($ this-> result );
  59. Return $ row;
  60. }
  61. /* Return query result row as an associated array .*/
  62. Function fetchArray (){
  63. $ Row = @ pg_fetch_array ($ this-> result );
  64. Return $ row;
  65. }
  66. /* Return total number of queries executed
  67. Lifetime of this object. Not required,
  68. Interesting nonetheless .*/
  69. Function numQueries (){
  70. Return $ this-> querycount;
  71. }
  72. }
  73. ?>
  • 1
  • 2
  • Next Page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.