PostgreSQL
以下将说明使用Apache+PHP3+PostgreSQL作为基于Web的数据库平台的安装和配置方法。关于Apache、PHP3和PostgreSQL的更多内容可以从软件的附带文档、Linux的HOWTO文件以及以下站点处找到:Apache: http://www.apache.org
PHP3: http://www.php.net
PostgreSQL: http://www.postgresql.org
1. PostgreSQL的安装和设置
1.1 获得源程序
PostgreSQL最新版本的源程序可以在http://www.postgresql.org找到。目前的最新版本是6.5.X。以下以6.4版为例说明安装方法。
1.2 准备工作
编译PostgreSQL需要3.75版以上的GNU make (用gmake -v检查版本号) ,2.7.2版以上的 GNU C(用gcc -v 检查版本号)以及bison和flex(通常这两种工具都已经安装) 。
PostgreSQL的默认安装位置为/usr/local/pgsql/,系统文件约需3-10M空间。附带的测试程序在运行时需要约20M空间,所以安装时应注意预留足够的空间,建议/usr/local/pgsql/ 目录下保证有50M以上空间。另外展开和编译源程序约需30-60M空间。
对多用户的应用环境,建议设置一个专用用户名,例如 postgres :
$ su 首先登录为root
# /usr/sbin/adduser postgres
另外PostgreSQL使用了System V的共享内存机制。FreeBSD默认状态不支持该机制。如使用中的内核的设置文件中无以下项目则需追加后重新编译内核:
options SYSVSHM
options SYSVSEM
options SYSVMSG
建保存源程序的目录/usr/local/src/pgsql和安装目录/usr/local/pgsql:
#mkdir /usr/local/pgsql
#chown postgres:postgres /usr/local/pgsql
#mkdir /usr/local/src
#mkdir /usr/local/src/pgsql
#chown postgres:postgres /usr/local/src/pgsql
1.3 编译
以postgres用户登录, 解压缩源程序:# su postgres
$ tar -xzvf /tmp/postgresql-v6.4.tar.gz
[假设下载文件保存在/tmp目录下]
完成后应生成一个postgresql-v6.4目录,下面开始编译:
$ cd /usr/local/src/pgsql/postgresql-v6.4/src
$ ./configure --with-mb=EUC_CN
其中,--with-mb=指定系统默认字符编码。除GB码(EUC_CN) 外,还可以指定为日语(EUC_JP)、韩语(EUC_KR) 、台湾(EUC_TW) 、UNICODE、MULE_INTERNAL、LATIN1等。
注意,如使用6.3.2版需用--with-template=... 指定操作系统,具体内容见源程序附带的说明文件。
configure如顺利完成,将生成GNUmakefile,Makefile.global和Makefile.port等文件。 $ gmake all在编译结束后将显示以下信息:All of PostgreSQL is successfully made. Ready to install。
安装:$ gmake install
正常完成后,PostgreSQl的执行文件和库文件等将被安装到/usr/local/pgsql目录下。
安装附带文档:
$ gmake install-man
$ cd /usr/local/src/postgresql-v6.4/doc
$ make install
至此PostgreSQL的编译安装已经完成,下面开始初期设置。
1.4 初期设置
设置环境变量:
如使用的shell为bash, 则在.bashrc中添加以下命令:
PATH="$PATH":/usr/local/pgsql/bin
export POSTGRES_HOME=/usr/local/pgsql
export PGLIB=$POSTGRES_HOME/lib
export PGDATA=$POSTGRES_HOME/data
export MANPATH="$MANPTH":$POSTGRES_HOME/man
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIB"
然后执行 source ~/.bashrc
如使用的shell为csh/tcsh则在.cshrc中添加以下命令:
setenv PATH="$PATH":/usr/local/pgsql/bin
setenv POSTGRES_HOME=/usr/local/pgsql
setenv PGLIB=$POSTGRES_HOME/lib
setenv PGDATA=$POSTGRES_HOME/data
setenv MANPATH="$MANPTH":$POSTGRES_HOME/man
setenv LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIB"
然后执行 source ~/.cshrc
以上环境变量是所有使用数据库的用户都需要设置的。
数据库目录的初始化:
$ initdb
可以使用的参数:
pgdata=/pgsql/db 指定数据库目录,默认使用环境变量PGDATA指定的位置
pgencoding=EUC_CN 指定数据库的字符编码,默认使用configure时指定的编码。
需要注意的是执行initdb的用户将拥有所建数据库目录的管理权。
使用以下命令启动PostgreSQL:
$ postmaster -S
运行测试程序:
$ cd test/regression
$ gmake all runtest
如测试程序能正常运行则说明PostgreSQL能正常运行。
为了让 PostgreSQL在系统启动时能自动启动,需作以下变更:
首先以root登录
$ su
Linux: 在/etc/rc.d/rc.local中追加以下内容:
POSTGRESDIR=/usr/local/pgsql
if [ -x $POSTGRESDIR/bin/postmaster -a
d $POSTGRESDIR/data ];then
rm -f /tmp/s.PGSQL.5432
su - postgres -c "postmaster -S -i"
echo -n 'postmaster'
fi
FreeBSD: 在/usr/local/etc/rc.d中建立名为
pgsql.sh的文件,内容为:
#! /bin/sh
POSTGRESDIR=/usr/local/pgsql
if [ -x $POSTGRESDIR/bin/postmaster
-a -d $POSTGRESDIR/data ];then
rm -f /tmp/s.PGSQL.5432
su - postgres -c "postmaster -S -i"
echo -n 'postmaster'
fi
修改pgsql.sh文件的权限:
# chmod 755 pgsql.sh
现在PostgreSQL的安装和设定基本已经完成了,但是目前为止能使用数据库的用户只有postgres。
为使其他用户可以使用数据库,需要登录数据库用户和生成用户数据。
如通过Web服务器Apache进行查询的用户,用户名为nobody:
% createuser nobody
Enter user's postgres ID or RETURN to use unix
user ID: 1000 ->1000
Is user "nobody" allowed to create dataase(y/n)n
Is user "nobody" allowed to add users?(y/n)n
createuser: nobody was successfully added
删除用户可以使用命令destroyuser 用户名
PostgreSQL可以同时管理多个数据库(但数据库之间不能进行join等操作)。新建数据库:
$ createdb 数据库名
新建的数据库将被放在/usr/local/pgsql/data/base的同名目录下(环境变量PGDATA指定路径的base目录下)。如省略数据库名参数,将自动以用户名作为数据库名。
例如 名为db1的数据库将被保存在/usr/local/pgsql/data/base/db1目录下。也可使用initlocation命令指定其他位置:
$ initlocation /pgsql/data
$ export PGDATA2=/pgsql/data
$ createdb -D PGDATA2 db2
则数据库db2将被保存在/pgsql/data目录下。另外PostgreSQL 6.4版可以在建立数据库时指定文字编码:createdb -E “字符编码” “字符编码” 参见configure、initdb。
删除数据库:destroydb 数据库名
PostgreSQL最基本的数据库管理工具是
pgsql. 基本使用方法:pgsql 数据库名
1.5 安全设置:
PostgreSQL提供了基于主机的认证方式host based authentication(HBA) 、基于口令的用户身份认证和用户操作权限设置等安全机制。
1.5.1 HBA方式和基于口令的用户身份认证方式
HBA方式的设置文件为pg_hba.conf。其格式为:
host DBNAME IP_ADDRESS ADDRESS_MASK USRAUTH [AUTH_ARGUMENT]
host: 固定标志,不能修改
DBNAME: 数据库名,all代表所有数据库
IP_ADDRESS,ADDRESS_MASK:指定IP地址,也可指定子网,如192.168.10.0/255.255.255.0
USERAUTH: 对用户的认证方式,包括ident(RFC1413) 、trust(不进行认证) 、reject (拒绝符合条件的访问)、password [passwd_file](根据flat file口令文件进行认证) 、 crypt(使用PostgreSQL的系统数据库pg_shadow进行认证)、kbr4/kbr5(Kerberos V4/V5认证) 。如在作为Web 数据库使用, 使用passwd口令文件进行本地登录时可如下设置:
Host all 127.0.0.1 255.255.255.255 password passwd
passwd文件默认位置为/usr/local/pgsql/data, 有数据库管理权限的用户可以使用
pg_passwd命令管理该文件(注意:flat file口令认证方式采用明文传送口令,故要从网络上其他主机登录,建议使用crypt认证方式)。
1.5.2 用户操作权限设置
使用SQL命令grant/revoke可以设置用户/用户组可否使用select/insert/update/rule命令。
根据SQL标准,一个数据表文件(table) 在刚建立时只有建立该表文件的用户有权访问。要分配给用户访问权限,可以使用grant命令:
grant on to
权限种类:all,select,insert,update,delete,rule
table名:被设置用户访问权限的表文件
用户/组:public(所有用户) 或用户名/组名
revoke命令作用与grant命令相反,用法如下:
revoke on from
table已设置的用户权限可以使用pgsql的\z 命令查看。
2. Apache+PHP3的安装及设置
2.1 源程序
Apache和PHP3的最新版本源程序可以从http://www.apache.org/和http://www.php.net/找到。
以下以Apache 1.3.6和PHP3 3.0.5 为例。
2.2 编译
假设Apache和PHP3的源程序都保存在/tmp目录下,首先登录为root:
$ su
# cd /usr/local/src
# tar -xzvf /tmp/apache_1.3.6.tar.gz
# ./configure
# cd /usr/local/src
# tar -xzvf /tmp/php3-3.0.5.tar.gz
# cd php-3.0.5
# ./configure --with-pgsql --with-apache=
../apache_1.3.6 --enable-track-vars
# make
# make install
最后一步将建立/usr/local/src/apache_1.3.6/src/modules/php3目录,并将以下文件
复制到该目录下:
Makefile.libdir libmodphp3.a mod_php3.c php_version.h
Makefile.tmpl libphp3.module mod_php3.h
编译和安装Apache:
设置环境变量LD_LIBRARY_PATH csh和tcsh shell使用以下命令:
# setenv LD_LIBRARY_PATH /usr/local/pgsql/lib
sh和bash使用如下命令:
# LD_LIBRARY_PATH=/usr/local/pgsql/lib
# export LD_LIBRARY_PATH
然后
# cd /usr/local/src/apache_1.3.6
# ./configure --activate-module=src/modules/php3/libphp3.a
# make
# make install
2.3 修改设置文件
# cd /usr/local/src/php-3.0.5
# cp php3.ini-dist /usr/local/lib/php3.ini
将/usr/local/apache/etc/srm.conf中以下两行内容前的#号删除,
如果以.php3作为PHP3文件的扩展名,则将.phtml 更改为 .php3:
#AddType application/x-httpd-php3 .php3
#AddType application/x-httpd-php3-source .phps
在srm.conf文件的DirectoryIndex下增加 index.php3:
DirectoryIndex index.html index.php3
2.4 启动Apache
# /usr/local/apache/sbin/apachectl start
apachectl start: httpd started
在本地机上启动网络浏览器,地址栏输入http://localhost/(或在其他计算机上的浏览器地址栏输入Apache服务器地址),如能显示出Apache的开始画面则说明Apache已经安装成功。下面测试PHP3模块工作是否正常:
# cd /usr/local/apache/share/htdocs/
# ln -s /usr/local/src/php-3.0.5 .
然后在浏览器的地址栏输入
http://localhost/php-3.0.5/examples/date.php3
观察php3文件的运行结果是否正常。如日期函数能显示正确结果,则说明PHP3模块工作正常。
至此,基于Apache Web服务器和服务器端脚本语言PHP3的PostgreSQL数据库系统的安装已经完成 :-)

Calculating the total number of elements in a PHP multidimensional array can be done using recursive or iterative methods. 1. The recursive method counts by traversing the array and recursively processing nested arrays. 2. The iterative method uses the stack to simulate recursion to avoid depth problems. 3. The array_walk_recursive function can also be implemented, but it requires manual counting.

In PHP, the characteristic of a do-while loop is to ensure that the loop body is executed at least once, and then decide whether to continue the loop based on the conditions. 1) It executes the loop body before conditional checking, suitable for scenarios where operations need to be performed at least once, such as user input verification and menu systems. 2) However, the syntax of the do-while loop can cause confusion among newbies and may add unnecessary performance overhead.

Efficient hashing strings in PHP can use the following methods: 1. Use the md5 function for fast hashing, but is not suitable for password storage. 2. Use the sha256 function to improve security. 3. Use the password_hash function to process passwords to provide the highest security and convenience.

Implementing an array sliding window in PHP can be done by functions slideWindow and slideWindowAverage. 1. Use the slideWindow function to split an array into a fixed-size subarray. 2. Use the slideWindowAverage function to calculate the average value in each window. 3. For real-time data streams, asynchronous processing and outlier detection can be used using ReactPHP.

The __clone method in PHP is used to perform custom operations when object cloning. When cloning an object using the clone keyword, if the object has a __clone method, the method will be automatically called, allowing customized processing during the cloning process, such as resetting the reference type attribute to ensure the independence of the cloned object.

In PHP, goto statements are used to unconditionally jump to specific tags in the program. 1) It can simplify the processing of complex nested loops or conditional statements, but 2) Using goto may make the code difficult to understand and maintain, and 3) It is recommended to give priority to the use of structured control statements. Overall, goto should be used with caution and best practices are followed to ensure the readability and maintainability of the code.

In PHP, data statistics can be achieved by using built-in functions, custom functions, and third-party libraries. 1) Use built-in functions such as array_sum() and count() to perform basic statistics. 2) Write custom functions to calculate complex statistics such as medians. 3) Use the PHP-ML library to perform advanced statistical analysis. Through these methods, data statistics can be performed efficiently.

Yes, anonymous functions in PHP refer to functions without names. They can be passed as parameters to other functions and as return values of functions, making the code more flexible and efficient. When using anonymous functions, you need to pay attention to scope and performance issues.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
