Use PostgreSQL database for PHP Development

Source: Internet
Author: User
Tags unix domain socket
Document directory
  • Use PostgreSQL database for PHP Development
Use PostgreSQL database for PHP Development

Author:Heiyeshuwu

 

Use PostgreSQL database for PHP Development

Note: The following content is more about how to install the environment. There is almost no actual development content. You can consider whether to read this article.

PostgreSQL databases should be the starting star in the database field. Some of these functions are even better than commercial databases such as Oracle. In the Open Source world, they can be said to be very good, and even MySQL is far behind it. It supports JDBC, ODBC, DBI, and other database connection methods. It can basically interact with all popular programming languages and is suitable for any programming developer.

The official introduction may be more informative: http://www.pgsqldb.org/pgsqldoc-8.0c/preface.html
Certificate ---------------------------------------------------------------------------------------------------------------------------------------------
What is PostgreSQL?
PostgreSQL is an object relational database management system (ORDBMS) based on ipvs developed by the computer department of UC Berkeley and version 4.2 ). Many of the leading concepts of ipvs appear in commercial databases very late.

PostgreSQL was the heir to the original Berkeley code's open source code. It supports most SQL: 2003 standards and provides many other modern features:

Complex Query
Foreign key
Trigger
View
Transaction integrity
Multi-version Concurrency Control
Similarly, PostgreSQL can be expanded in many ways, such as by adding a new one:

Data Type
Function
Operator
Aggregate functions
Index Method
Process Language

In addition, because of the flexible license, anyone can use, modify, and distribute PostgreSQL for free for any purpose, whether it is private, commercial, or academic research.
Certificate ----------------------------------------------------------------------------------------------------------------------------------------------

Today, I want to try to use PostgreSQL for PHP development, so I just need to install one.

The first consideration was to install the latest version 8.0.3 in Windows. But think about it, the actual development platform may be mostly in Unix/Linux, so I was prepared to develop it under FreeBSD, we upgraded FreeBSD5.4 yesterday. We need to hurry up with ports.

Cd/usr/ports/database/postgresql80-server
Cat Makefile
The latest version is 8.0.3.
Make install
In the process of waiting for a long time, you need to download the program gmake, and download the program sql-8.0.3.tar.bz2 and other files, which will only be completed after half a day. Haha, I'm very excited. Let's take a look:
/Usr/local/etc/rc. d/postgresql. sh start (this file may not be named like this)
Dizzy. Why is it wrong? It cannot be started anyway. It seems that you have to install it manually. uninstall it first:
Cd/usr/ports/database/postgresql80-server
Make deinstall
Alas, copy the file:
Cd/usr/ports/distfiles
Mv./postgresql-8.0.3.tar.bz2/usr/local/src
Decompress:
Cd/usr/local/src
Bunzip2-d./postgresql-8.0.3.tar.gz2
Tar xvf./postgresql-8.0.3.tar
Decompressed successfully:
Cd./postgresql-8.0.3
Install the configuration file in our old directory:
./Configure -- prefix =/usr/local/pgsql
Gmake
Wait for the process, and later, dizzy, how again error?
Prompt that the file system is full... -_-#
I am using a virtual machine and the space allocation is unreasonable. Check if I can add some space to/usr.
I went to google to search for information and found only a tunefs command to solve this problem. I didn't understand it after reading it for a long time. Link:
Http://www.freebsd.org.cn/snap/doc/zh_CN.GB2312/books/handbook/configtuning-disk.html
Think about it or forget it. It's not enough to load it to other partitions. Let's take a look at the space situation:
% Df
Filesystem 1K-blocks Used Avail Capacity Mounted on
/Dev/ad0s1a 507630 55060 411960 12%/
Devfs 1 1 0 100%/dev
/Dev/ad0s1e 507630 70072 396948 15%/tmp
/Dev/ad0s1f 2025982 1960440-96536 105%/usr
/Dev/ad0s1d 507630 76340 390680 16%/var
Then install/var:
./Configure -- prefix =/var/pgsql
Gmake
Gmake install
The process went smoothly. Add a user running pg:

Adduser
Username: S
Full name: PostgreSQL Server User
Uid (Leave empty for default ):
Login group [S]:
Login group is ipvs. Invite postgres into other groups? []:
Login class [default]:
Shell (sh csh tcsh bash nologin) [sh]:/bin/csh
Home directory [/home/postgres]:
Use password-based authentication? [Yes]:
Use an empty password? (Yes/no) [no]: yes
Lock out the account after creation? [No]:
Username: S
Password: <blank>
Full Name: PostgreSQL Server User
Uid: 1004
Class:
Groups: postgres
Home:/home/postgres
Shell:/bin/csh
Locked: no

Create a directory for storing databases:
Mkdir/var/pgsql/data
Assign ownership to postgres users:
Chown postgres/var/pgsql/data
Switch to postgres:
Su-postgres
Initialize the database:
/Var/pgsql/bin/initdb-D/var/pgsql/data
Start running:
/Var/pgsql/bin/postmaster-D/var/pgsql/data> logfile 2> & 1 &
Dizzy, what a bunch of errors !!!
Postmaster does not know where to find the server configuration file.
You must specify the -- config-file or-D invocation option or set the PGDATA environment variable.
No configuration file specified. Check the configuration file again?
Find/-name "postgresql. conf"
/Var/pgsql/data/postgresql. conf
Oh, the configuration file was created when the database was initialized, and the following code should be written:
./Postmaster -- config-file =/var/pgsql/data/postgresql. conf
Dizzy, why is there a new error?
Oh, there was no content for configuring postgresql. conf. Please configure the important content as follows:

#---------------------------------------------------------------------------
# FILE LOCATIONS
#---------------------------------------------------------------------------
 
# The default values of these variables are driven from the-D command line
# Switch or PGDATA environment variable, represented here as ConfigDir.
# Data_directory = 'configdir' # use data in another directory
Data_directory = '/var/pgsql/data'
# Pai_file = 'configdir/pg_mirror.conf '# the host-based authentication file
Export _file = '/var/pgsql/data/pg_mirror.conf'
# Ident_file = 'configdir/pg_ident.conf '# the IDENT configuration file
Ident_file = '/var/pgsql/data/pg_ident.conf'

As you can see, a database file directory and two configuration file paths are specified.
Restart:
./Postmaster -- config-file =/var/pgsql/data/postgresql. conf
LOG: database system was shut down at 01:20:24 CST
LOG: checkpoint record is at 0/A38D20
LOG: redo record is at 0/A38D20; undo record is at 0/0; shutdown TRUE
LOG: next transaction ID: 678, And next OID: 17231
LOG: database system is ready

OK. Okay. See where I can access the default port 5432 first:
Telnet 192.168.0.215 5432
What's going on? Google it. Oh, it turns out that the local link is only allowed by default. You need to modify the configuration file:
Vi/var/pgsql/data/postgresql. conf

Modify the following content:
#---------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#---------------------------------------------------------------------------
 
#-Connection Settings-
 
# Listen_addresses = 'localhost' # what IP interface (s) to listen on;
# Defaults to localhost, '*' = any
Listen_addresses = '*'
# Port = 5432
If you do not see it, you can change the default localhost of the listening address to *, that is, allow all. Telnet after restart. The screen is black and there is no problem.
Haha, now I can use PHP to connect.

Open the pgsql extension in php and output phpinfo () to see if pgsql can be supported. After a look, you can write the code to see it:

<? Php
$ Link_str = "host = 192.168.0.215 port = 5432 dbname = test user = s password = postgres ";
$ Link_id = pg_connect ($ link_str );
Print_r ($ link_id );
?>

Save it as pgsql. php, open the browser: http: // localhost/pgsq. php, haha, wait for the link ID to come out!
Oh, what's going on? Error:

Warning: pg_connect () [function. pg-connect]: Unable to connect to PostgreSQL server: FATAL: no pg_assist.conf entry for host "192.168.0.216", user "s", database "test" in E: /Web/www/Temp/pgsql/pg. php on line 3

Take a closer look, pg_mirror.conf entry for host "192.168.0.216", oh, it turns out that access from this IP address is not allowed in this configuration file. It's really safe!
It seems that this pg_cmd.conf must also be set. Open it quickly:
Vi/var/pgsql/data/pg_hba.conf
An IP address is added as follows:
# Type database user CIDR-ADDRESS METHOD
 
# "Local" is for Unix domain socket connections only
Local all trust
# IPv4 local connections:
Host all 127.0.0.1/32 trust
Host all 192.168.0.216 trust
# IPv6 local connections:
Host all: 1/128 trust
No, I added:
Host all 192.168.0.216 trust
If you want to allow an IP address segment, you can add it to 192.168.0.1/24 or something.

Start postgresql again, and my browser connects again. The browser outputs:
Resource id #1
^ _ ^. It's successful!

Hurry to the next windows Management Tools, go to: http://www.pgadmin.org/download a pgAdmin III back, after installation, start, Add Server, succeeded. It is easy to use and has a fight with MysqlCC. It also supports Chinese characters.

Basically, there is nothing here. In addition, a pgsql feature means that if the same user logs on to the server from multiple IP addresses, only one user is allowed to perform the operation. Other users cannot perform the operation. Otherwise, an error occurs. For example, after I log on to my XP using pgAdmin, I log on to the server to perform the operation:
/Var/pgsql/bin/createdb db1
Createdb: database creation failed: ERROR: source database "template1" is being accessed by other users
No. An error occurred.

From the above, pgsql is really safe, killing many security problems in the cradle, And the configuration is also rich, although the installation process is a little complicated. In fact, I can understand the manual by looking at the manual. I just don't want to read the manual.

Dizzy. After talking for a long time, I didn't know how to develop PHP. Haha, it is important to build an environment. As for the features of development and pgsql, I will study it slowly.

Add some nice things:

Officially recommended installation process, but it is not practical:
Bytes -------------------------------------------------------------------------------------------
./Configure
Gmake
Su
Gmake install
Adduser postgres
Mkdir/usr/local/pgsql/data
Chown postgres/usr/local/pgsql/data
Su-postgres
/Usr/local/pgsql/bin/initdb-D/usr/local/pgsql/data
/Usr/local/pgsql/bin/postmaster-D/usr/local/pgsql/data> logfile 2> & 1 &
/Usr/local/pgsql/bin/createdb test
/Usr/local/pgsql/bin/psql test
Bytes ------------------------------------------------------------------------------------------

PostgreSQL Chinese manual: http://www.pgsqldb.org/twiki/bin/view/PgSQL/PgDocList
PostgreSQL Chinese Forum: http://bbs.pgsqldb.com
PostgreSQL Official Website: http://www.postgresql.org

Author: heiyeluren
WriteTime:

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.