您的位置:首页 > 运维架构 > Linux

在Linux环境下编译安装配置PostGIS/PostgreSQL全过程

2012-01-01 12:53 721 查看
在Linux环境下编译安装配置PostGIS/PostgreSQL全过程 (2009-08-27 17:57)
分类:
PostgreSQL

来源:3SDN.Net 作者:3SDN原创

[版权声明:本文系3SDN原创,在明文注明原文链接的情况下允许转载,否则将追究其法律责任。

一、前期准备

1、PostgreSQL源码,笔者撰稿时版本为8.4.0,文件名:postgresql-8.4.0.tar.gz

下载地址:http://www.postgresql.org/ftp/source/v8.4.0/

2、PostGIS源码,当前版本为1.3.6,文件名:postgis-1.3.6.tar.gz

下载地址:http://postgis.refractions.net/download/

3、Geos源码,当前版本为3.1.1,文件名:geos-3.1.1.tar.bz2

下载地址:http://trac.osgeo.org/geos/

4、Proj源码,当前版本为4.6.1,文件名:proj-4.6.1.tar.gz

下载地址:http://trac.osgeo.org/proj/

二、Linux环境下编译安装

以Linux的Ubuntu发行版为例,其它发行版类似。

将下载的四个源码复制到同一个文件夹,如/home/spider/source/,用cd命令进入到源码文件夹。

1、编译安装PostgreSQL

创建安装目录,如:

mkdir /usr/local/pgsql

解压:

tar zxf postgresql-8.4.0.tar.gz

进入目录:

cd postgresql-8.4.0

配置:

./configure –prefix=/usr/local/pgsql

编译:

make

安装:

make install

(说明,以下步骤类似,不再添加注释)

2、编译安装Geos

mkdir /usr/local/geos

tar jxf geos-3.1.1.tar.bz2

cd geos-3.1.1

./configure –prefix=/usr/local/geos

make

make install
3、编译安装Proj

mkdir /usr/local/proj4

tar zxf proj-4.6.1.tar.gz

cd proj-4.6.1

./configure –prefix=/usr/local/proj4

make

make install
4、编译安装PostGIS

mkdir /usr/local/postgis

tar zxf postgis-1.3.6.tar.gz

cd postgis-1.3.6

./configure –prefix=/usr/local/postgis --with-pgsql=/usr/local/pgsql/bin/pg_config --with-proj=/usr/local/proj4 --with-geos=/usr/local/geos/bin/geos-config

make

make install
5、PostGIS/PostgreSQL配置

(1)创建PostgreSQL的用户,设定密码

adduser postgresp

(2)创建PostgreSQL的数据库目录,修改目录的权限属性

mkdir /usr/local/pgsql/data

chown -R postgres /usr/local/pgsql

(3)以postgres用户登陆

su postgres

(4)初始化数据库集群

/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data

(5)启动数据库服务器

pg_ctl start -D /usr/local/pgsql/data –l pgsql.log

(6)设置远程可访问数据库

vi /usr/local/pgsql/data/postgresql.conf

把listen_address = 'localhost' 改为 listen_address = '*'

(vi编辑器简单入门:按ESC切换输入状态和指令状态,按a进入编辑,在指令状态,按“:x”保存退出)

vi /usr/local/pgsql/data/pg_hba.conf

在文件最后加入:

host  all  all  192.168.1.0/24  password

停止数据库服务

/usr/local/pgsql/bin/pg_ctl stop -D /usr/local/pgsql/data

重新启动数据库

/usr/local/pgsql/bin/pg_ctl start -D /usr/local/pgsql/data –l pgsql.log

(7)创建数据库

切换到postgres用户

su postgres

创建数据库gisdb

/usr/local/pgsql/bin/createdb gisdb

增加plpgsql语言

/usr/local/pgsql/bin/createlang plpgsql gisdb

(8)增加动态链接库的搜索路径

vi /etc/ld.so.conf

在最后添加四行:

/usr/local/lib

/usr/local /proj4/lib

/usr/local /geos/lib

/usr/local /pgsql/lib

运行ldconfig命令,使以上修改生效:

/sbin/ldconfig

(9)导入PostGIS空间支持文件

/usr/local/pgsql/bin/psql -d gisdb -f /usr/local/postgis/share/lwpostgis.sql

/usr/local/pgsql/bin/psql -d gisdb -f /usr/local/postgis/share/spatial_ref_sys.sql

(10)导入Shapefile到数据库

准备好shapefile文件,如shpfile.shp,shpfile.shx,shpfile.dbf并cd在该目录下。

/usr/local/postgis/bin/shp2pgsql shpfile shpfile>shpfile.sql

shp2pgsql命令的参数分别为shapefile的文件名(不含扩展名),表名,输出的sql文件名。

/usr/local/pgsql/bin/psql -d gisdb -f shpfile.sql

可以用QGIS查看刚才添加的shapefile图层。

打开“图层”>>“添加postgis图层”->“新建”>>输入连接名称、localhost或127.0.0.1或服务器名、数据库名、端口号(5432)、用户名(postgres)及密码>>“测试连接”,如果连接成功>>“连接”>>选择表>>“添加”。
至此,在Linux下编译安装配置PostGIS/PostgreSQL就完成了。

[以下内容属于原创,针对上述转载内容的实务补充]
1、 在/etc/init.d/ 目录下创建 postgresql 启动脚本
脚本内容:
#!/bin/bash

#

# chkconfig: 2345 85 15

# description: Starts and stops the PostgreSQL 8.4 database server
# Source function library.

if [ -f /etc/rc.d/functions ];

then

. /etc/init.d/functions

fi
# PostgreSQL Service script for Linux
start()

{

echo $"Starting PostgreSQL 8.4: "

su - postgres -c "LD_LIBRARY_PATH=/usr/local/pgsql/lib /usr/local/pgsql/bin/pg_ctl -w start -D \"/usr/local/pgsql/data\" -l \"/usr/local/pgsql/data/pg_log/startup.log\""
if [ -e "/usr/local/pgsql/data/postmaster.pid" ]

then

echo "PostgreSQL 8.4 started successfully"

exit 0

else

echo "PostgreSQL 8.4 did not start in a timely fashion, please see /usr/local/pgsql/data/pg_log/startup.log for details"

exit 1

fi

}
stop()

{

echo $"Stopping PostgreSQL 8.4: "

su - postgres -c "LD_LIBRARY_PATH=/usr/local/pgsql/lib /usr/local/pgsql/bin/pg_ctl stop -m fast -w -D \"/usr/local/pgsql/data\""

}
# See how we were called.

case "$1" in

start)

start

;;

stop)

stop

;;
restart|reload)

stop

sleep 3

start

;;

condrestart)

if [ -f "/usr/local/pgsql/data/postmaster.pid" ]; then

stop

sleep 3

start

fi

;;

status)

su - postgres -c "LD_LIBRARY_PATH=/usr/local/pgsql/lib /usr/local/pgsql/bin/pg_ctl status -D \"/usr/local/pgsql/data\""

;;

*)

echo $"Usage: /usr/local/pgsql/installer/server/startupcfg.sh {start|stop|restart|condrestart|status}"

exit 1

esac

2、添加自启加服务
#chkconfig --add postgresql
#chkconfig --level 3 postgresql on

至此,postgresql将随系统启动自动启动服务守护进程
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: