PostGIS安装及ShapeFile数据导入
目录
1. 简介
1.1 安装环境:ContOS6.8, PostGIS9.4, PostpreSQL9.4, QGIS2.17
1.2 说明:
和
http://blog.csdn.net/caoshiying/article/details/51778207
1.2.2 安装过程中要求安装依赖时一律是:yes
2. 安装PostgreSQL
2.1 下载文件
2.2 上传文件到ContOS(远程数据库)
2.3 安装
1. yum install pgdg-redhat94-9.4-1.noarch.rpm
2. yum install postgresql94-server postgresql94-contrib
2.4 配置
2.4.1 初始化并启动服务
service postgresql-9.4 initdb
chkconfig postgresql-9.4 on
2.4.2 编辑pg_hba.conf
cd /var/lib/pgsql/9.4/data
vim pg_hba.conf
保存并退出
1. # TYPE DATABASE USER ADDRESS METHOD
2.
3. # “local” is for Unix domain socket connections only
4. local all all peer
5. # IPv4 local connections:
6. host all all 0.0.0.0/0 MD5
7. # IPv6 local connections:
8. host all all ::1/128 ident
9. # Allow replication connections from localhost, by a user with the
10. # replication privilege.
11. #local replication postgres peer
12. #host replication postgres 127.0.0.1/32 ident
13. #host replication postgres ::1/128 ident
2.4.3 编辑postgresql.conf文件
vim postgresql.conf
保存并退出
1. # – Connection Settings –
2.
3. listen_addresses = ‘localhost’ # what IP address(es) to listen on;
4. # comma-separated list of addresses;
5. # defaults to ‘localhost’; use ‘*’ for all
6. # (change requires restart)
7. port = 5432 # (change requires restart)
8. max_connections = 100 # (change requires restart)
2.4.4 编辑环境变量
vim /etc/profile
source /etc/profile
1. HOSTNAME=`/bin/hostname 2>/dev/null`
2. HISTSIZE=1000
3. if [ “$HISTCONTROL” = “ignorespace” ] ; then
4. export HISTCONTROL=ignoreboth
5. else
6. export HISTCONTROL=ignoredups
7. fi
8.
9. export PGHOME=/usr/pgsql-9.4
10. export PGDATA=/var/lib/pgsql/9.4/data
export PATH=$PGHOME/bin:$PATH
3. 安装PostGIS
首选检查PostGIS是否存在源列表中:
1. postgres=# \q
2. bash-4.1$ exit
3. exit
4. [root@db etc]# yum list | grep -i postgis
5. postgis.x86_64 1.3.6-1.el6.rf rpmforge
6. postgis-utils.x86_64 1.3.6-1.el6.rf rpmforge
7. postgis2_94.x86_64 2.1.8-1.rhel6 pgdg94
8. postgis2_94-client.x86_64 2.1.8-1.rhel6 pgdg94
9. postgis2_94-debuginfo.x86_64 2.1.8-1.rhel6 pgdg94
10. postgis2_94-devel.x86_64 2.1.8-1.rhel6 pgdg94
11. postgis2_94-docs.x86_64 2.1.8-1.rhel6 pgdg94
12. postgis2_94-utils.x86_64 2.1.8-1.rhel6 pgdg94
13. [root@db etc]#
尝试安装:
1. [root@db etc]# yum install postgis2_94
测试建立空间数据库:
1. -bash-4.1$ psql -c “CREATE DATABASE test01;”
2. CREATE DATABASE
3. -bash-4.1$ psql -d test01 -c “CREATE EXTENSION postgis;”
4. CREATE EXTENSION
5. -bash-4.1$ psql -d test01
6. psql (9.4.8)
可以输入 “help”来获取帮助信息,下面是查询已安装的PostGIS的版本
1. test01=# select postgis_full_version();
2. 注意: Function postgis_topology_scripts_installed() not found. Is topology support enabled and topology.sql installed?
3. postgis_
4. full_version
5.
6. ——————————————————————————–
7. ——————————————————————————–
8. —–
9. POSTGIS=“2.1.8 r13780” GEOS=“3.4.2-CAPI-1.8.2 r3921” PROJ=”Rel. 4.8.0, 6 March
10. 2012” GDAL=”GDAL 1.9.2, released 2012/10/08” LIBXML=”2.7.6” LIBJSON=”UNKNOWN” RA
11. STER
12. (1 行记录)
13.
14. test01=#
4. 安装pgRouting二进制文件
4.1安装
yum install pgrouting_94
5. 安装ogrfdw二进制文件
安装:
yum install ogr_fdw94
6. 在数据库中启用扩展
6.1 修改用户密码
1. [root@db etc]# cat /etc/passwd | grep -i postgres
2. postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
3. [root@db etc]#
如果输出为空则表示用户不存在,需要新建用户。如果已经存在,则需要修改用户密码。用户密码需要分两步修改。首先修改系统的postgres用户的密码:
1. [root@db etc]# passwd postgres
2. Changing password for user postgres.
3. New password:
4. Retype new password:
5. passwd: all authentication tokens updated successfully.
6. [root@db etc]#
切换到postgres用户:
1. [root@db etc]# su postgres
2. bash-4.1$
尝试重启或启动PostgreSQL服务
1. bash-4.1$ pg_ctl restart
2. pg_ctl: PID file “/var/lib/pgsql/9.4/data/postmaster.pid” does not exist
3. Is server running?
4. starting server anyway
5. pg_ctl: could not read file “/var/lib/pgsql/9.4/data/postmaster.opts”
6. bash-4.1$ pg_ctl start
7. server starting
8. bash-4.1$ < 2016-06-28 23:46:04.688 CST >LOG: redirecting log output to logging collector process
9. < 2016-06-28 23:46:04.688 CST >HINT: Future log output will appear in directory “pg_log”.
10.
11. bash-4.1$
注意服务启动成功时命令行没有返回用户的bash,按一下回车即可。
进入psql命令行:
1. bash-4.1$ psql
2. psql (9.4.8)
3. Type “help” for help.
4.
5. postgres=#
修改数据库的postgres用户的密码:
1. alter user postgres with password ‘最好与系统的用户密码相同’;
命令中的红色文字请自行更改为您自己的密码,然后执行命令,如下所示:
1. postgres=# alter user postgres with password ‘不告诉你^_^^_^’;
2. ALTER ROLE
3. postgres=#
退出测试一下:
1. postgres=# \q
2. bash-4.1$ psql -h 192.168.8.155 -p 5432 -d postgres -U postgres -W
3. Password for user postgres:
4. psql (9.4.8)
5. Type “help” for help.
6.
7. postgres=#
至此可以用工具远程登录数据库系统了。
创建一个数据库并安装扩展
1. bash-4.1$ pg_ctl restart
2. pg_ctl: PID file “/var/lib/pgsql/9.4/data/postmaster.pid” does not exist
3. Is server running?
4. starting server anyway
5. pg_ctl: could not read file “/var/lib/pgsql/9.4/data/postmaster.opts”
6. bash-4.1$ pg_ctl start
7. server starting
8. bash-4.1$ < 2016-06-28 23:46:04.688 CST >LOG: redirecting log output to logging collector process
9. < 2016-06-28 23:46:04.688 CST >HINT: Future log output will appear in directory “pg_log”.
10.
11. bash-4.1$
注意服务启动成功时命令行没有返回用户的bash,按一下回车即可。
进入psql命令行:
1. su postgres
2. /usr/pgsql-9.4/bin/psql -p 5432.
3. CREATE DATABASE gistest;
4. \connect gistest;
接下来安装扩展
5. CREATE EXTENSION postgis;
6. CREATE EXTENSION postgis_topology;
7. CREATE EXTENSION ogr_fdw;
7. 导入ShapeFile文件
PostGIS连接数据库
导入数据
查询导入数据
转载自:https://blog.csdn.net/weixin_38843590/article/details/79315947