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

Linux环境下PostgreSQL与PostGIS的安装与配置

2013-04-10 11:09 603 查看
Linux环境下PostgreSQL与PostGIS的安装、配置与简单应用

一、软件环境

1. Red Hat Linux 9

2. PostgreSQL-8.2.5

3. PostGIS-1.3.2

4. Proj-4.5.0 (to provide coordinate reprojection support within PostGIS)

5. GEOS-3.0.0Orc4 (to provide geometry tests and operations within PostGIS)

二、安装与配置

(一)PostgreSQL的安装

1. 下载源码并解压

# tar zxf postgresql-8.2.5.tar.gz

2. 进入PostgreSQL-8.2.5目录

2.1. 将安装目录设置为/opt/pgsql

# ./configure --prefix=/opt/pgsql

2.2. 编译、安装

# make

# make install

3. 增加PostgreSQL的最高访问用户并设定密码

# adduser pgadmin

# passwd pgadmin

4. 创建PostgreSQL的数据库目录,并修改目录的用户属性

# mkdir /opt/pgsql/data

# chown -R pgadmin /opt/pgsql

5. 修改PostgreSQL最高访问用户pgadmin的bash_profile

# vi /home/pgadmin/.bash_profile

添加:

PGLIB=/opt/pgsql/lib

PGDATA=/opt/pgsql/data

PATH=$PATH:/opt/pgsql/bin

MANPATH=$MANPATH:/opt/pgsql/man

export PGLIB PGDATA PATH MANPATH

6. 以用户pgadmin登录

# su - pgadmin

7. 初始化数据库存储区(initialize a database storage area)

$ /opt/pgsql/bin/initdb -D /opt/pgsql/data

一个数据库存储区也叫一个数据库群集(a database cluster,是一个正在运行的数据库服务器实例所管理的数据库的集合)

8. 启动数据库服务器(Starting the Database Server)

$ postmaster >pgsql.log 2>&1 &

(注: ">pgsql.log" 的意思是将postmaster的输出重定向到pgsql.log文件中去, "2>&1"的意思是如果有错误,错误信息也重定向输出到pgsql.log中去,最后一个 "&" 的意思是在后台运行postmaster服务程序)

或者

$ postmaster -D /opt/pgsql/data >pgsql.log 2>&1 &

或者

$ postgres -D /opt/pgsql/data >pgsql.log 2>&1 &

或者

$ pg_ctl start -D /opt/pgsql/data –l pgsql.log

9. 创建数据库

$ /opt/pgsql/bin/createdb testdb

若PostgreSQL 会返回 "CREATED DATABASE" 的信息,表明数据库建立完成

$ /opt/pgsql/bin/psql testdb //用交互工具 psql 连接进入数据库

testdb=# \i /home/postgresql/test_table.sql //执行外部SQL脚本文件

testdb=# \l //列出所有数据库

testdb=# \dt //列出被连接数据库中的表

10. 设置远程可访问数据库

$ vi /opt/pgsql/data/postgresql.conf

将listen_address = 'localhost' 改为 listen_address = '*'

$ vi /opt/pgsql/data/pg_hba.conf

在文件最后加入:

host  all  all  192.168.1.0/24  password

重新启动数据库

$ /opt/pg/bin/pg_ctl stop -D /opt/pgsql/data

$ /opt/pg/bin/postmaster -i -D /opt/pgsql/data>logfile 2>&1 &

这样就可以远程访问数据库了,如下边的命令:

$ psql -h 192.168.1.216 -p 5432 -d testdb -U user1

将以用户user1的身份去访问主机为192.168.1.216上名为testdb的数据库。

11. 简单应用(用于测试PostgreSQL数据库是否正常工作)

11.1. 进入数据库操作界面

$ /opt/pg/bin/psql testdb

11.2. 创建用户

testdb=# create user user1 password '123456';

11.3. 创建数据库

testdb=# create database db1 owner user1;

11.4 创建表

testdb=# create table tab1(name varchar(10));

11.5 改变表的属主

testdb=# alter table tab1 owner to user1;

(二)PostGIS的安装

1. 先安装Proj4和GEOS

1.1. 安装Proj-4.5.0

# tar zxf proj-4.5.0.tar.gz

# cd proj-4.5.0

# ./configure --prefix=/opt/proj4 //设置安装位置为/opt/proj4

# make

# make install

1.2. 安装Geos-3.0.0Orc4

# tar jxf geos-3.0.0rc4.tar.bz2

# cd geos-3.0.0rc4

# ./configure --prefix=/opt/geos3 //设置安装位置为/opt/geos3

# make

# make install

2. 安装PostGIS-1.3.2

# tar zxf postgis-1.3.2.tar.gz

# cd postgis-1.3.2

# ./configure --prefix=/opt/postgis--with-pgsql=/opt/pgsql/bin/pg_config --with-proj=/opt/proj4--with-geos=/opt/geos3/bin/geos-config

(设置安装位置为/opt/postgis,并且使用Proj4与GEOS3)

# make

# make install

3. PostGIS的配置

3.1. 更改用户到pgadmin,创建测试数据库

$ createdb testgisdb //创建测试数据库

$ createlang plpgsql testgisdb //使数据库识别PL/pgSQL语言

3.2. 增加动态链接库的搜索路径

# vi /etc/ld.so.conf

在文件末尾增加下面四行:

/usr/local/lib

/opt/proj4/lib

/opt/geos3/lib

/opt/pgsql/lib

然后运行ldconfig使刚加入的库路径生效:

# /sbin/ldconfig

3.3. 进入目录/opt/postgis/share,为数据库增加空间支持:

$ psql -d trydb -f lwpostgis.sql //装入预定义的PostGIS空间类型与函数

$ psql -d testgisdb -f spatial_ref_sys.sql //装入预定义的空间坐标参照系

4. 简单使用PostGIS

$ psql testgisdb

testgisdb=# create user user2 password '123456';

testgisdb=# alter database testgisdb owner to user2;

testgisdb=# alter table spatial_ref_sys owner to user2;

testgisdb=# alter table geometry_columns owner to user2;

5. 导入shp文件:

5.1. 将 test1.shp test1.shx test1.dbf 复制到/opt/postgis/bin目录下

# chmod +x test1.shp

# chmod +x test1.shx

# chmod +x test1.dbf

# chown -R pgadmin /opt/postgis/bin

5.2. 更改用户到 pgadmin

$ /opt/postgis/bin/shp2pgsql test1 test1>test1.sql

$ psql -d testgisdb -f test1.sql

$ psql testgisdb

testgisdb=# alter table test1 owner to user2;

6. 客户端登陆使用

这里用的是QuantumGIS,即QGIS

运行软件,依次打开“图层”->“添加postgis图层”->“新建”

输入名称、主机的IP地址或服务器名、数据库名、端口号(5432)、用户名及密码

点“测试连接”,看能能否正常通讯。

最后连接,选择表,然后点“添加”,添加图层

7. 设置开机自动启动PostgreSQL

7.1. 进入.../postgresql-8.2.5/contrib/start-scripts 目录,将linux文件改名复制到init.d目录下:

# cp linux /etc/rc.d/init.d/postgres

7.2. 进入/etc/rc.d/init.d目录,编辑postgres:

=============================

prefix = /opt/pgsql

PGDATA = "/opt/pgsql/data"

PGUSER = pgadmin

PGLOG = "$PGDATA/pgsql.log"

=============================

# chmod +x postgres

7.3. 分别在/etc/rc.d/rc3.d目录和/etc/rc.d/rc3.d目录,输入命令:

# ln -sf /etc/rc.d/init.d/postgres S11postgres

重启Linux,即可。

===完毕====
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: