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数据库系统的安装已经完成 :-)

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

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

Dreamweaver CS6
Visual web development tools