PostGIS 2.0 Install

3 minute read

背景

应同事需求, 写一篇PostGIS的安装文档.

安装环境 :

CentOS 5.x 64位
PostgreSQL 9.1.3
PostGIS 2.0

一、安装PostgreSQL

新建用户(用户名随意)

useradd postgis

配置环境变量

vi /home/postgis/.bash_profile
# add by digoal
export PS1="$USER@`/bin/hostname -s`-> "
export PGPORT=1997
export PGDATA=/home/postgis/pg_root
export LANG=en_US.utf8
export PGHOME=/home/postgis/pgsql
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib
export DATE=`date +"%Y%m%d%H%M"`
export PATH=$PGHOME/bin:$PATH:.
export MANPATH=$PGHOME/share/man:$MANPATH
alias rm='rm -i'
alias ll='ls -lh'

下载PostgreSQL源码包

wget http://ftp.postgresql.org/pub/source/v9.1.3/postgresql-9.1.3.tar.bz2

解压

tar -jxvf postgresql-9.1.3.tar.bz2

编译

postgis@db-172-16-3-33-> cd postgresql-9.1.3
postgis@db-172-16-3-33-> ./configure --prefix=/home/postgis/pgsql --with-pgport=1997 --with-perl --with-python --with-openssl --with-pam --without-ldap --with-libxml --with-libxslt --enable-thread-safety --with-wal-blocksize=16 && gmake world

安装

sudo gmake install-world

初始化数据库

postgis@db-172-16-3-33-> mkdir $PGDATA
postgis@db-172-16-3-33-> initdb -D $PGDATA -E UTF8 --locale=C -W

修改配置文件

postgis@db-172-16-3-33-> vi $PGDATA/pg_hba.conf
# add by digoal
host all all 0.0.0.0/0 md5

postgis@db-172-16-3-33-> vi $PGDATA/postgresql.conf
listen_addresses = '0.0.0.0'
max_connections = 2000
unix_socket_directory = '.'
unix_socket_permissions = 0700
tcp_keepalives_idle = 60
tcp_keepalives_interval = 10
tcp_keepalives_count = 10
shared_buffers = 2048MB
maintenance_work_mem = 512MB
max_stack_depth = 8MB
shared_preload_libraries = 'pg_stat_statements'
vacuum_cost_delay = 10ms
bgwriter_delay = 10ms
wal_level = hot_standby
synchronous_commit = off
wal_sync_method = fdatasync
wal_buffers = 16384kB
wal_writer_delay = 10ms
checkpoint_segments = 128
archive_mode = on
archive_command = '/bin/date'
max_wal_senders = 32
wal_sender_delay = 10ms
wal_keep_segments = 256
hot_standby = on
max_standby_archive_delay = 900s
max_standby_streaming_delay = 900s
wal_receiver_status_interval = 10s
hot_standby_feedback = off
random_page_cost = 1.5
effective_cache_size = 16384MB # 与内存大小一致
log_destination = 'csvlog'
logging_collector = on
log_directory = '/var/applog/pg_log/postgis/1997'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
log_file_mode = 0600
log_truncate_on_rotation = on
log_rotation_age = 1d
log_rotation_size = 10MB
log_min_duration_statement = 1000ms
log_checkpoints = on
log_lock_waits = on
log_statement = 'ddl'
track_activity_query_size = 2048
autovacuum = on
log_autovacuum_min_duration = 0
deadlock_timeout = 1s
custom_variable_classes = 'pg_stat_statements'
pg_stat_statements.max = 1000
pg_stat_statements.track = all

新建日志目录

[root@db-172-16-3-33 postgresql-9.1.3]# mkdir -p /var/applog/pg_log/postgis/1997
[root@db-172-16-3-33 postgresql-9.1.3]# chown -R postgis:postgis /var/applog/pg_log/postgis

启动数据库

postgis@db-172-16-3-33-> pg_ctl start
server starting
postgis@db-172-16-3-33-> LOG:  loaded library "pg_stat_statements"

二、安装PostGIS2.0依赖包

下载安装包

[root@db-172-16-3-33 soft_bak]# wget ftp://ftp.remotesensing.org/gdal/gdal-1.9.1.tar.gz
[root@db-172-16-3-33 soft_bak]# wget http://download.osgeo.org/geos/geos-3.3.4.tar.bz2
[root@db-172-16-3-33 soft_bak]# wget http://download.osgeo.org/proj/proj-4.8.0.tar.gz
[root@db-172-16-3-33 soft_bak]# wget http://oss.metaparadigm.com/json-c/json-c-0.9.tar.gz

安装

[root@db-172-16-3-33 soft_bak]# tar -zxvf gdal-1.9.1.tar.gz
[root@db-172-16-3-33 soft_bak]# cd gdal-1.9.1
[root@db-172-16-3-33 gdal-1.9.1]# ./configure --prefix=/opt/gdal-1.9.1
[root@db-172-16-3-33 gdal-1.9.1]# make
[root@db-172-16-3-33 gdal-1.9.1]# make install

[root@db-172-16-3-33 geos-3.3.4]# tar -jxvf geos-3.3.4.tar.bz2
[root@db-172-16-3-33 geos-3.3.4]# cd geos-3.3.4
[root@db-172-16-3-33 geos-3.3.4]# ./configure --prefix=/opt/geos-3.3.4
[root@db-172-16-3-33 geos-3.3.4]# make
[root@db-172-16-3-33 geos-3.3.4]# make install

[root@db-172-16-3-33 soft_bak]# tar -zxvf proj-4.8.0.tar.gz
[root@db-172-16-3-33 soft_bak]# cd proj-4.8.0
[root@db-172-16-3-33 proj-4.8.0]# ./configure --prefix=/opt/proj-4.8.0
[root@db-172-16-3-33 proj-4.8.0]# make
[root@db-172-16-3-33 proj-4.8.0]# make install

[root@db-172-16-3-33 soft_bak]# tar -zxvf json-c-0.9.tar.gz
[root@db-172-16-3-33 soft_bak]# cd json-c-0.9
[root@db-172-16-3-33 json-c-0.9]# ./configure --prefix=/opt/json-c-0.9
[root@db-172-16-3-33 json-c-0.9]# make
[root@db-172-16-3-33 json-c-0.9]# make install

使用rpm安装以下包

libxml2-devel
libxml2
glibc-common (iconv)
GTK+2.0

修改启动postgresql的系统用户环境变量, 把这些LIB加入到LD_LIBRARY_PATH中.

vi /home/postgis/.bash_profile
export LD_LIBRARY_PATH=/opt/gdal-1.9.1/lib:/opt/geos-3.3.4/lib:/opt/json-c-0.9/lib:/opt/proj-4.8.0/lib:$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib

这些修改好后, 需要应用环境变量并重启数据库.

su - postgis
. ~/.bash_profile
pg_ctl stop -m fast
pg_ctl start

三、安装PostGIS 2.0

下载安装包

wget http://www.postgis.org/download/postgis-2.0.0.tar.gz

安装

[root@db-172-16-3-33 soft_bak]# tar -zxvf postgis-2.0.0.tar.gz
[root@db-172-16-3-33 soft_bak]# cd postgis-2.0.0
[root@db-172-16-3-33 postgis-2.0.0]# ./configure --prefix=/opt/postgis-2.0.0 --with-pgconfig=/home/postgis/pgsql/bin/pg_config --with-gdalconfig=/opt/gdal-1.9.1/bin/gdal-config --with-geosconfig=/opt/geos-3.3.4/bin/geos-config --with-xml2config=/usr/bin/xml2-config --with-projdir=/opt/proj-4.8.0 --with-libiconv=/usr/bin --with-jsondir=/opt/json-c-0.9 --with-gui --with-raster --with-topology --with-gettext=no
[root@db-172-16-3-33 postgis-2.0.0]# gmake
[root@db-172-16-3-33 postgis-2.0.0]# gmake install

查看安装是否正确 :

postgis@db-172-16-3-33-> cd $PGHOME/share/extension/
postgis@db-172-16-3-33-> ll|grep postgis
-rw-r--r-- 1 root root 478K Jun  1 17:44 postgis--2.0.0alpha1--2.0.0.sql
-rw-r--r-- 1 root root 478K Jun  1 17:44 postgis--2.0.0alpha2--2.0.0.sql
-rw-r--r-- 1 root root 478K Jun  1 17:44 postgis--2.0.0alpha3--2.0.0.sql
-rw-r--r-- 1 root root 478K Jun  1 17:44 postgis--2.0.0alpha4--2.0.0.sql
-rw-r--r-- 1 root root 478K Jun  1 17:44 postgis--2.0.0alpha5--2.0.0.sql
-rw-r--r-- 1 root root 478K Jun  1 17:44 postgis--2.0.0alpha6--2.0.0.sql
-rw-r--r-- 1 root root 478K Jun  1 17:44 postgis--2.0.0beta1--2.0.0.sql
-rw-r--r-- 1 root root 478K Jun  1 17:44 postgis--2.0.0beta2--2.0.0.sql
-rw-r--r-- 1 root root 478K Jun  1 17:44 postgis--2.0.0beta3--2.0.0.sql
-rw-r--r-- 1 root root 478K Jun  1 17:44 postgis--2.0.0beta4--2.0.0.sql
-rw-r--r-- 1 root root 478K Jun  1 17:44 postgis--2.0.0rc1--2.0.0.sql
-rw-r--r-- 1 root root 478K Jun  1 17:44 postgis--2.0.0rc2--2.0.0.sql
-rw-r--r-- 1 root root 4.0M Jun  1 17:44 postgis--2.0.0.sql
-rw-r--r-- 1 root root  185 Jun  1 17:44 postgis.control
-rw-r--r-- 1 root root 280K Jun  1 17:44 postgis_topology--2.0.0alpha1--2.0.0.sql
-rw-r--r-- 1 root root 280K Jun  1 17:44 postgis_topology--2.0.0alpha2--2.0.0.sql
-rw-r--r-- 1 root root 280K Jun  1 17:44 postgis_topology--2.0.0alpha3--2.0.0.sql
-rw-r--r-- 1 root root 280K Jun  1 17:44 postgis_topology--2.0.0alpha4--2.0.0.sql
-rw-r--r-- 1 root root 280K Jun  1 17:44 postgis_topology--2.0.0alpha5--2.0.0.sql
-rw-r--r-- 1 root root 280K Jun  1 17:44 postgis_topology--2.0.0alpha6--2.0.0.sql
-rw-r--r-- 1 root root 280K Jun  1 17:44 postgis_topology--2.0.0beta1--2.0.0.sql
-rw-r--r-- 1 root root 280K Jun  1 17:44 postgis_topology--2.0.0beta2--2.0.0.sql
-rw-r--r-- 1 root root 280K Jun  1 17:44 postgis_topology--2.0.0beta3--2.0.0.sql
-rw-r--r-- 1 root root 280K Jun  1 17:44 postgis_topology--2.0.0beta4--2.0.0.sql
-rw-r--r-- 1 root root 280K Jun  1 17:44 postgis_topology--2.0.0rc1--2.0.0.sql
-rw-r--r-- 1 root root 280K Jun  1 17:44 postgis_topology--2.0.0rc2--2.0.0.sql
-rw-r--r-- 1 root root 277K Jun  1 17:44 postgis_topology--2.0.0.sql
-rw-r--r-- 1 root root  169 Jun  1 17:44 postgis_topology.control
-rw-r--r-- 1 root root 8.0K Jun  1 17:44 postgis_topology--unpackaged--2.0.0.sql
-rw-r--r-- 1 root root  77K Jun  1 17:44 postgis--unpackaged--2.0.0.sql

四、使用PostGIS 2.0

创建一个空间测试库

postgis@db-172-16-3-33-> psql postgres postgis -h 127.0.0.1
psql (9.1.3)
Type "help" for help.
postgres=# create database digoal template template0 encoding 'UTF8' ;
CREATE DATABASE

在空间测试库中加载postgis 和 postgis_topology 模块

使用超级用户创建这两个模块

postgres=# \c digoal postgres
digoal=# create extension postgis;
CREATE EXTENSION
digoal=# create extension postgis_topology;
CREATE EXTENSION

现在就可以使用postgis 了.

参考

http://www.postgis.org

http://workshops.opengeo.org/postgis-intro

依赖包

http://www.gdal.org/index.html

http://trac.osgeo.org/geos/

http://trac.osgeo.org/proj/

libxml2-devel

libxml2

glibc-common (iconv)

GTK+2.0

http://oss.metaparadigm.com/json-c/

Flag Counter

digoal’s 大量PostgreSQL文章入口