centos安装使用postgress
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# 官网下载
wget https://ftp.postgresql.org/pub/source/v10.1/postgresql-10.1.tar.gz
# 自己的镜像下载
wget http://tar.matosiki.site/tar/postgresql-10.1.tar.gz
# 安装c语言环境
yum -y install gcc
yum -y install gcc-c++
yum -y install readline-devel zlib zlib-devel uuid uuid-devel readline-devel
|
编译
1
2
3
4
5
6
7
8
9
10
|
mkdir -p /usr/local/pgsql
tar -zxvf postgresql-10.1.tar.gz -C /usr/local/pgsql
cd /usr/local/pgsql/
mv postgresql-10.1/* /usr/local/pgsql/
./configure LDFLAGS=-L/usr/local/lib CPPFLAGS=-I/usr/local/include --prefix=/usr/local/postgres --with-ossp-uuid
make
make install
|
添加用户
1
2
3
4
5
6
7
8
9
10
|
adduser postgres
# 设置密码为postgres
passwd postgres
mkdir -p /data/postgres
chown -R postgres /data/postgres
chmod -R 700 /data/postgres
|
安装插件
1
2
3
4
5
6
7
8
9
10
|
cd /usr/local/pgsql/contrib/adminpack
make
make install
cd /usr/local/pgsql/contrib/uuid-ossp
make
make install
# 为`libuuid.so.16`建立链接
ln -s /usr/local/lib/libuuid.so.16 /usr/lib64
|
完成后检查一下权限,如果出现无法找到该库文件的错误,就是由于该文件的权限引起
初始化数据库
1
2
3
|
su postgres
cd /usr/local/postgres/bin
./initdb -D /data/postgres
|
配置数据库
1
2
3
4
5
6
|
vim /data/postgres/postgresql.conf
# 更改内容
listen_addresses = '*'
vim /data/postgres/pg_hba.conf
# 最下边添加一行
host all all 0.0.0.0/0 md5
|
启动服务
1
|
/usr/local/postgres/bin/postgres -D /data/postgres
|
此时shell
被独占,需要重新开启一个shell
并切换到postgres
账号进行后续操作
进入postgres
库
1
2
3
4
5
6
7
8
9
10
11
12
|
cd /usr/local/postgres/bin
su postgres
./psql postgres
\password postgres
# 输入新密码
# 创建uuid扩展
create extension "uuid-ossp";
# 退出终端
\q
|
使用root权限 开启5432端口
1
2
|
firewall-cmd --add-port=5432/tcp --permanent
firewall-cmd --reload
|
设置服务自启动
创建文件
1
|
vim /usr/lib/systemd/system/postgres.service
|
内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
[Unit]
Description=PostgreSQL database server
After=network.target
[Service]
Type=forking
User=postgres
Group=postgres
PIDFile=/data/postgres/postmaster.pid
OOMScoreAdjust=-1000
Environment=PGSTARTTIMEOUT=270
Environment=PGDATA=/data/postgres
ExecStart=/usr/local/postgres/bin/pg_ctl start -D ${PGDATA} -s -w -t ${PGSTARTTIMEOUT}
ExecStop=/usr/local/postgres/bin/pg_ctl stop -D ${PGDATA} -s -m fast
ExecReload=/usr/local/postgres/bin/pg_ctl reload -D ${PGDATA} -s
TimeoutSec=300
[Install]
WantedBy=multi-user.target
|
安装 PostGIs 扩展
安装其它依赖
1
|
yum install -y libxml2 libxml2-devel json-c json-c-devel sqlite-devel
|
编译安装 proj
下载地址:http://download.osgeo.org/proj/
官网5.2.0下载地址 https://download.osgeo.org/proj/proj-5.2.0.tar.gz
1
2
3
4
5
6
|
cd ~
wget http://tar.matosiki.site/tar/proj-5.2.0.tar.gz
tar -zxvf proj-5.2.0.tar.gz
cd proj-5.2.0
./configure --prefix=/usr/local/proj
make && make install
|
编译安装geos
下载地址:http://trac.osgeo.org/geos/
官方3.7.1地址 http://download.osgeo.org/geos/geos-3.7.1.tar.bz2
1
2
3
4
5
6
|
wget http://tar.matosiki.site/tar/geos-3.7.1.tar.bz2
bzip2 -d geos-3.7.1.tar.bz2
tar vxf geos-3.7.1.tar
cd geos-3.7.1
./configure --prefix=/usr/local/geos
make && make install
|
编译安装gdal
下载地址:http://download.osgeo.org/gdal/
官网2.4下载地 http://download.osgeo.org/gdal/2.4.0/gdal-2.4.0.tar.gz
1
2
3
4
5
|
tar -zxvf gdal-2.4.0.tar.gz
wget http://tar.matosiki.site/tar/gdal-2.4.0.tar.gz
cd gdal-2.4.0
./configure --prefix=/usr/local/gdal
make && make install
|
编译postgis
https://download.osgeo.org/postgis/source/postgis-2.5.1.tar.gz
1
2
3
4
5
|
wget http://tar.matosiki.site/tar/postgis-2.5.1.tar.gz
tar -zxvf postgis-2.5.1.tar.gz
cd postgis-2.5.1
./configure --with-pgconfig=/usr/local/postgres/bin/pg_config --with-geosconfig=/usr/local/geos/bin/geos-config --with-projdir=/usr/local/proj --with-gdalconfig=/usr/local/gdal/bin/gdal-config --with-jsondir=/usr/include/json-c
make & make install
|
到postgresql
源码目录的contrib/fuzzystrmatch
下直接make & make install
安装该插件
重启数据库,执行以下查询验证插件是否安装成功
1
2
3
|
/usr/local/pgsql/contrib/fuzzystrmatch
./configure --with-pgconfig=/usr/local/postgres/bin/pg_config --with-geosconfig=/usr/local/geos/bin/geos-config --with-projdir=/usr/local/proj --with-gdalconfig=/usr/local/gdal/bin/gdal-config --with-jsondir=/usr/include/json-c
make & make install
|
1
|
select \* from pg_available_extensions where name like 'postgis%';
|
在navicat
的查询分析器里执行
1
|
CREATE EXTENSION fuzzystrmatch;
|
解决依赖问题
1
|
vim /etc/ld.so.conf.d/postgis.conf
|
写入以下内容
1
2
3
|
/usr/local/gdal/lib/
/usr/local/proj/lib/
/usr/local/geos/lib/
|
执行ldconfig
命令使配置生效
1
2
3
|
ldconfig
su postgres
cd /usr/local/postgres/bin
|
创建一个用于演示的空间模板数据库
1
|
./createdb template_postgis
|
对postgis
库赋予空间扩展
1
2
3
|
./psql -f /usr/local/postgres/share/contrib/postgis-2.5/postgis.sql -d template_postgis
./psql -f /usr/local/postgres/share/contrib/postgis-2.5/spatial_ref_sys.sql -d template_postgis
|
该库不要做任何改动,仅做为库的模板使用,以后要新建有 PostGIS 功能的库就以此为模板即可
库中的spatial_ref_sys
表也不要改动或删除
安装PostGIS
的座标转换扩展,支持以下座标类型转换
- BD2GCJ:百度坐标转火星坐标
- GCJ2BD:火星坐标转百度坐标
- WGS2GCJ:84(gps)坐标转火星,常用把自己的数据加偏移叠加到高德,谷歌,esri 底图上可用
- GCJ2WGS:火星转 84
- BD2WGS:百度转 84
- WGS2BD:84 转百度
扩展下载地址:https://github.com/FreeGIS/postgis_LayerTransform
将库中的sql
文件里的sql
语句在template_postgis
上执行一次即可。