读书人

liunx环境停源码安装PostgreSQL+PostG

发布时间: 2013-03-26 09:54:34 作者: rapoo

liunx环境下源码安装PostgreSQL+PostGIS

?

1.软件准备

?? ?操作系统版本

?? ?CentOS5.5

?? ?源代码包

?? ? postgresql-9.0.2

?? ?postgis-1.5.2

?? ?proj-4.7.0

?? ?geos-3.2.2

?? ? libxml2-2.6.26

?

2.软件安装

?? ? ? (1) PostgreSQL安装

?? ? ? ?下载PostgreSQL源代码

1.软件准备

?? ?操作系统版本

?? ?CentOS5.5

?? ?源代码包

?? ?postgresql-9.0.2

?? ?postgis-1.5.2

?? ?proj-4.7.0

?? ?geos-3.2.2

?? ?libxml2-2.6.26

?

2.软件安装

?? ? ? (1) PostgreSQL安装

?? ? ? ?下载PostgreSQL源代码包

?? ? ? ?# wget ftp://ftp2.cn.postgresql.org/postgresql/source/v9.0.2/postgresql-9.0.2.tar.bz2 <==下载PostgreSQL9.0.2源码包

?? ? ? ?# tar -jxvf postgresql-9.0.2.tar.bz2 <==解压源码包

?? ? ? ?# cd postgresql-9.0.2 <==进入到源码包文件夹

?? ? ? ?#./configure --prefix=/usr/local/pgsql ?<==配置软件的安装目录

?? ? ? ?#gmake <==编译软代码

?? ? ? ?All of PostgreSQL is successfully made. Ready to install. <==编译成功显示的信息

?? ? ? ?/*******************************编译需要具备的条件*********************************************************

?? ? ? ? ?  gmake(make)版本最好是3.79.1 或者更高版本,CentOS5.5中make的版本号为

?? ? ? ? ? ?  # rpm -q ?make ?<==这个必须有

?? ? ? ? ?    make-3.81-3.el5

?? ? ? ? ?  readline库(默认),命令行编辑功能,这个库虽然不是必须的(可以在配置阶段通过 --without-readline选项来取消),但是强烈建议安装readline,

?? ? ? ?    使用psql时,可以使用方向键来选择和编辑前面键入的命令,CentOS5.5中readline版本为

?? ? ? ? ?    # rpm -qa | grep readline ?<这个可以有,最好有

?? ? ? ? ? ?  readline-5.1-3.el5

?? ? ? ? ?    readline-devel-5.1-3.el5

?? ? ? ? ?  zlib库(默认),主要用于pg_dump和pg_restore两个操作的数据压缩功能,这个库也不是必须的(可以在配置阶段通过--without-zlib选项来取消),

?? ? ? ? ? ?  CentOS5.5中zlib安装情况

?? ? ? ? ?    # rpm -qa | grep zlib

?? ? ? ? ?    zlib-devel-1.2.3-3

?? ? ? ? ?    zlib-1.2.3-3

?? ? ?    在编译程序前必须要安装这些包,否则无法通过编译,此外,还有在配置阶段还有很多可选包,可以通过./configure --help查看,这里不多介绍。

?? ? ?**********************************************************************************************************/

?? ? ? ?#gmake install <==安装软件到指定的配置阶段prefix指定的目录下

?? ? ? ?#adduser postgres ?<==添加postgres用户,postgreSQL的默认超级管理员

?? ? ? ?#mkdir /usr/local/pgsql/data ?<==创建存放数据的目录

?? ? ? ?#chown postgres /usr/local/pgsql/data ?<==改变文件拥有者

?? ? ? ?#su - postgres <==切换到postgres用户

?? ? ? ?#/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data ?<==初始化磁盘上的数据库的存储区间

?? ? ? ?#/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data >logfile 2>&1 &?

?? ? ? ? ? ? ? ? ? ? ?<==指定数据目录启动数据库服务器(输出重定向到logfile,并且运行在后台,也可以用pg_ctl来启动)

?? ? ? ?#/usr/local/pgsql/bin/createdb test <== 创建测试数据库,确认安装顺利完成

?? ? ? ?#/usr/local/pgsql/bin/psql test ?<== 连接进数据库,可以SQL了

?? ? ? ?安装完成后,最好配置一下动态连接库的搜索路径和环境变量,即在~./bash_profile添加如下内容:

?? ? ? ? ? ? LD_LIBRARY_PATH=/usr/local/pgsql/lib <==动态连接库的所在目录

?? ? ? ? ? ? export LD_LIBRARY_PATH?

?? ? ? ? ? ? PATH=/usr/local/pgsql/bin:$PATH <==为了方便使用,添加命令的搜索路径

?? ? ? ? ? ? export PATH

?? ?自此,PostgreSQL9.0.2安装完成。

?

?

?? ?(2) postgis的安装

?? ? ? ? ? ? 首先下载源码包postgis-1.5.2,proj-4.7.0,geos-3.2.2,libxml2-2.6.26,虽然这三个库不是安装postgis强制的,但是,没有这三个包,

?

?? ? ? postgis一定程度上失去了空间数据库的意义。因为Proj4提供了投影的相关操作,如postgis中的transform()函数,geos则为postgis提供了很多拓扑

?

?? ? ? 检查功能的函数,如Touches(), Contains(), Disjoint() 还有一些空间操作函数,如Intersection(), union ?() 以及 Buffer()等 ,而Libxml2则提

?? ? ? 供了对GML和KML的操作函数,如ST_GeomFromGML(), ST_GeomFromKML()等,如果丧失了这样特性,空间数据库将会怎样!

?

?? ? ? 废话少说,下面开始安装

?? ?/***************安装proj**************************************/

?? ?# ./configure --prefix=/usr/local/proj ?<==配置proj,指定安装路径,其他默认

?? ?# make <==编译

?? ?# make install ?<==安装proj?

?? ?/***************安装geos**************************************/

?? ?# ./configure --prefix=/usr/local/geos ?<==配置GEOS,指定安装路径,其他默认

?? ?# make <==编译

?? ?# make install ?<==安装geos

?? ?将/usr/local/geos/lib添加到/etc/ld.so.conf中,然后运行/sbin/ldconfig,这样做的目的是/etc/ld.so.conf列出

?? ?的路径下的库文件缓存到/etc/ld.so.cache 以供使用,否则下面的编译可以会出现找不到xxx库的错误。

?? ?libxml2库,CentOS5.5已经安装,如果没有请自行安装,CentOS5.5中libxml2具体情况如下

?? ? ? ?# rpm -q libxml2

?? ? ? ?libxml2-2.6.26-2.1.2.8.el5_5.1

?? ?/*********前提条件具备了,下面开始安装PostGIS,软件是在postgres用户下安装的,所以下面命令提示符为$**********/

?? ?$ ./configure --with-pgconfig=/usr/local/pgsql/bin/pg_config --with-projdir=/usr/local/proj --with-geosconfig=/usr/local/geos/bin/geos_config

?? ? ? ? ? ?<==配置信息比较多,

?? ? ? ? ? ?<== --with-pgconfig是指定PostgreSQL的配置文件

?? ? ? ? ? ?<== --with-projdir指定proj的安装目录

?? ? ? ? ? ?<== --with-geosconfig则是指定geos库的配置文件,

?? ? ? ? ? ?<==另外还有--with-xml2config选项,如果libxml2是默认安装的则省略这个选项,否则还需要指定这个选项到libxml2的配置文件

?? ? ? ? ? ?<==熟悉windows环境下的postgis就会指定,将shape文件导入到数据库中还有个图形界面shp2pgsql-gui,这个可以用--with-gui指定

?? ?$ make <==编译

?? ?$ make install <==安装postgis

?? ?其实安装都是这几个步骤,熟悉linux的朋友只要./configure阶段注意一些配置信息既可顺利安装。

?

?

?

3 测试安装是否正确-----创建空间数据库。

?? ? ? ? ? 熟悉windows环境下postgis的朋友,都会注意到,安装了postgis后,pgsql中多了一个数据库template_postgis,这是个空间数据库的模

?? ? 板,其实就是个空间数据库。而在linux环境下通过源码安装的postgis,默认没有创建这个空间数据库,下面我就用创建者个模板空间数据库来验证

?

?? ? 上面的安装是否正确。一旦创建了一个空间数据库模板,以后每次创建空间数据库只要在这个模板空间数据库上创建就可以了,省时省力!

?? ? /*****************如果不熟悉pgsql相关命令的可以参考本博客的《创建PostGIS空间数据库》******************************/

?? ?# su - postgres <==切换到postgres用户下

?? ?$ createdb template_postgis <==创建数据库,此时,该数据库还没有具备空间特性

?? ?$ psql -f /usr/local/pgsql/share/contrib/postgis-1.5/postgis.sql -d template_postgis

?? ? ? ? ? ?<==执行postgis.sql脚本,创建相关空间数据库相关的函数,类型,操作符等

?? ? ? ? ? ?<==执行完这个脚本,该数据库就具有了空间特性了

?? ?$createdb [-U username] -T template_postgis my_spatial_db

?? ? ? ? ? ?<==下次再创建数据库,只要以这个模板就可以了,不必每次都执行这个脚本,-U指定用户名,默认就是postgres

?? ?$psql my_spatial_db <==连接到创建的空间数据库

?? ?my_spatial_db=# select postgis_full_version(); ? ? <== 查询postgis的版本信息,包含用到的三个库信息

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?postgis_full_version ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

?

?

?

?

?

?

?

?

?

读书人网 >操作系统

热点推荐